Series Editor, Head First<br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596102259/newinstance-20" target="_blank" rel="nofollow">Head Rush Ajax</a> and <a href="http://www.amazon.com/Head-First-Object-Oriented-Analysis-Design/dp/0596008678/ref=pd_bbs_sr_1/104-5348268-5670331?ie=UTF8&s=books&qid=1192568453&sr=8-1/newInstance-20" target="_blank" rel="nofollow">Head First OOA&D</a>
It seems to be a common practise for people to use interfaces to define constants (refer to "Effective Java" by Josh Bloch, Item 17 for further info).
Tony Morris
Java Q&A (FAQ, Trivia)
Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Originally posted by Tony Morris:
I assume you meant "common and poor practice" instead of just "common practice"?
Item 17 makes this clear:
"The constant interface pattern is a poor use of interfaces" - most informed people, including myself, concur with Bloch.
The better solution is Item 21, the Typesafe Enum Pattern.
The J2SE 5.0 enum keyword is merely a shortcut to writing such a construct.
Just thought I'd clarify.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
But I don't see what's wrong with putting real constants in an interface and refering to them as MyConstants.MINUTES_PER_HOUR
Tony Morris
Java Q&A (FAQ, Trivia)
Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Originally posted by Corey McGlone:
Although these lines of code are quite contrived, I feel that the second line is more readable than the first. I would, of course, implore that appropriate documentation was included and that the programmer imported only the static method(s) needed and no more, to avoid namespace pollution.
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Originally posted by Ko Ko Naing:
Just to share my point of view on it, I feel like for those who just start to learn Java, they won't know that Math class contains round(), cos() and random()... And they need to go back to the import statements above and figure it out...
But the first code provides with the class name "Math". In this case, they don't need to go back to the import statements above to know about the class... This kinda of "easy to read and understand" matters as well...![]()
Originally posted by Ko Ko Naing:
Just to share my point of view on it, I feel like for those who just start to learn Java, they won't know that Math class contains round(), cos() and random()... And they need to go back to the import statements above and figure it out...
But the first code provides with the class name "Math". In this case, they don't need to go back to the import statements above to know about the class... This kinda of "easy to read and understand" matters as well...![]()
Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
It seems to be a common practise for people to use interfaces to define constants (refer to "Effective Java" by Josh Bloch, Item 17 for further info).
But I don't see what's wrong with putting real constants in an interface and refering to them as MyConstants.MINUTES_PER_HOUR
Static Imports
Clients must qualify static members with class name (Math.PI). To avoid this, some programmers put constants in an interface and implement it. BAD - "Constant Interface Antipattern"
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
SCJP 1.4 / 5.0 - SCBCD 1.3 - SCWCD 1.4 - IBM 484
Originally posted by Vinicius Boson:
Hi folks,
Is static import when you use a fully path of a class, and so, you don�t need to import it ?
Like
Regards,
Originally posted by Corey McGlone:
This is an example of the anti-pattern folks are talking about.
SCJP 1.4 / 5.0 - SCBCD 1.3 - SCWCD 1.4 - IBM 484
Hahahahahahahahaha!john rambo wrote:. . . i wasnt even born that time . . .
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |