The best way to iterate (pre java 1.5)
for (final Iterator iter = myCollection.iterator(); iter.hasNext(); ) {
//code
}
Benefits:
- forces iter to have the right scope
- puts looping logic on one line (unlike while, or wose yet, do...while)
A gripe
javadoc should be able to translate < and > into < and > automatically. For actual html tags, you can just write <li> or whatever, but you can't put List<Dog>
in a javadoc comment. Javadoc should be able to tell the difference between html tags and parameterized types (not to mention mathematical logic, like myNum < .5). I just think "@param dogs a List<Dog> that are available for sale
" looks ugly.
Use Business-Level Helpers
In Better Faster Lighter Java, Tate and Gehtland discuss breaking dependencies (pp 54 ff). They describe the "train wreck" that can happen when you have things like "store.getAddress().getCountry().getState().getCity()
or ... address.country.state.city
... [especially when the code] reaches into many different packages." They suggest adding helper methods that do the reaching for you from the top layer so that clients of Store don't need to know about the intermediary classes.
I suggest an alternate solution: the use of very specialized helper classes. Such a class might have only one method: City getCity(Store store)
. They are even more useful when doing calculations rather than simple accesses. A class like this allows you to change the internal structure of Store without affecting its clients. If you later want to use database lookups or web-service lookups, or send an SMS to a guy that sits in your office to do lookups, you can simply replace the code for the helper. (Note that these helpers should NOT be Singletons, or you're going to create quite the bottleneck!)
No comments:
Post a Comment