Feng Shui Your Java Class

Just like some people clutter their aparments with abdominal exercise machines and informercial food processors, programmers also clutter their classes with well meaning, but ultimately useless features. You want to give the next visitor to your class a sense of positive energy.

Here are a few tips on making a visitor to your class feel at ease.

Constructors

Place all constructors together. Order them least complex to most complex.

Logically Group Methods

Put all Getters and Setters together (Get followed by Set)

Place similar methods in cohesive blocks. All overloaded methods related to each other should be grouped together. The class should be easily understandable to a new visitor, allowing the visitor to quickly navigate the different regions of the class.

Logically Group Class Variables

Place them all at the top (obviously!).

Place static variables above non static variables.

Group semantically similar variables, eg. all text fields should be grouped together.

Initialisation Methods

Place initialisation methods after constructor code in sequential order. Methods such as initComponents(), which are directly called from constructors, may be directly followed by initListeners(). Make sure that they maintain that sequential order in the class, as they have in the method calls.

Comments

Remove Redundant Comments.

Examples :

Unless your getter/setter method comments can add meaning to what the actual method does, then get rid of them. If you want to add comments about the member variable concerned then directly add comments to the member variable at the top of the class.

Note : In the final stages you may want to add comments to your getter and setter methods. But in the early stages of development leave them out because the code will be chopping and changing, and meaningful comments are a headache to maintain.

Common methods such as equals(..), toString() etc..

Place all implemented methods from the Object class at the bottom.

clone()
equals()
finalize()
hashcode()

If you are implementing the comparable interface, place the compare() method after the equals() method. A casual observer interested in the equality of an object may naturally be curious do its default sorting.

Place the toString() method at the very bottom of the class. The last method in a class is on some valuable real estate. Most visitors to a class that is especially large, will focus very intently on the beginning and the end of a class. The toString() method should be easily findable, because it is a method that is quite frequently modified.

Keep it Simple

Scrap overloaded methods that are not used. Nothing is more disorientating than stepping into a class and having to step through half a dozen constructors, or overloaded methods to get to the real meat. Get rid of them. They just add distraction and bloat.

Avoid becoming a code hoarder; someone who is sentimentally attached to a large complex piece of code that will never be used again, add noise and confusion to the software code.

Do not implement features that will not be used in the forseeable future, such as hashCode(). Cross that bridge when you get there (KIS -Keep It Simple principle).


Posted in Uncategorized | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

THREE_COLUMN_PAGE