Abstract: Do generics make the code easier or harder to read? Anything new can be challenging on the eyes, but time will tell whether generics will be as popular as we expect.
In my last newsletter, I mentioned that I like generics. To me, they improve the Java language by being more specific as to what is allowed in collections. I find writing code with generics quite easy, because IntelliJ IDEA assists me with it. However, John O'Sullivan mentioned that it took him "MUCH MUCH MUCH longer to read" the code to see what I was doing, and I think that John has a point.
This may be a problem. The cost of maintenance often dwarfs the cost of initially writing the code. Can generics help in maintenance? I cannot recall ever seeing a ClassCastException "in the field" due to incorrect types being put into a collection.
Here is the AbstractConfiguration class written without using
generics. By removing generics, I discovered that the
getAllProperties() would have been better off returning a
copy of a Map, rather than a Set of entries. This "design
flaw" was obscured by having so much text as a return code:
Set<Map.Entry<String, Object>>
.
So, here is your opportunity to have your say. Please think about whether you find that generics will improve maintenance or not. Remember that if the syntax bothers you, that is a matter of time before you are used to it. You can also do clever tricks with syntax highlighting. An IDE could even give you the option of hiding generics in the editor so that they do not bother you. Try thinking of technical reasons, rather than just looks.
I will send a summary of the results in my next newsletter, and might publish the full results on my webpage. Please indicate if you would like to remain anonymous in the results. If I do reveal your comments, they would only include your name and company, definitely not your email address.
import java.beans.*; import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; public abstract class AbstractConfigurationNoGenerics { /** A map of all the properties for the configuration */ private final Map properties = new HashMap(); private final Collection listeners = new CopyOnWriteArrayList(); /** Make a daemon timer to check for changes. */ private final Timer timer = new Timer(true); /** * This class has a timer to check periodically if the * configuration has changed. If it has, it reloads the * properties. This may cause the property change events to * fire. * * @param period number of milliseconds between checking for * property changes. */ protected AbstractConfigurationNoGenerics(int period) { timer.schedule(new TimerTask() { public void run() { checkForPropertyChanges(); } }, period, period); } /** * This method should be overridden to check whether the * properties could maybe have changed, and if yes, to reload * them. */ protected abstract void checkForPropertyChanges(); public final Object getProperty(String propertyName) { synchronized (properties) { return properties.get(propertyName); } } public Map getAllProperties() { synchronized (properties) { return new HashMap(properties); } } /** * Each time we set a property, we check whether it has changed * and if it has, we let the listeners know. */ protected final void setProperty(String propertyName, Object value) { synchronized (properties) { Object old = properties.get(propertyName); if ((value != null && !value.equals(old)) || value == null && old != null) { properties.put(propertyName, value); PropertyChangeEvent event = new PropertyChangeEvent(this, propertyName, old, value); for (Iterator it = listeners.iterator(); it.hasNext();) { PropertyChangeListener listener = (PropertyChangeListener) it.next(); listener.propertyChange(event); } } } } public void addPropertyChangeListener(PropertyChangeListener l) { listeners.add(l); } public boolean removePropertyChangeListener(PropertyChangeListener l) { return listeners.remove(l); } }
I am currently sitting in Citrusdal, about 2 hours drive from my home, and this newsletter will reach you via GPRS :-)
Kind regards
Heinz
We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)
We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.