bio photo

Check the following code fragment:

    // get post mode and default to post = true, when nothing is given
boolean post = true;
if (effectType.isSetPost()) {
if (PostType.FALSE == effectType.getPost()) {
post = false;
} else if (PostType.TRUE == effectType.getPost()) {
post = true;
}
}</pre>
Although there's no code duplicated in there (the root of all software evil), there is still some information duplicated. The comment repeats the same information as the code! And duplicate information is not good. If I want to change the default value I need to change two lines: the actual code and the comment. And maybe I forget to change one part and voila introduced inconsistence. Which comes back to me someday and is going to bite me.

So what to do, to make it still readable and easily changeable?

Just select "true" and in eclipse press "right mouse button" / refactor / extract constant. Enter a name and press Return to get something like this:

  /**
* default value for post/pre effect state.
*/
private static final boolean DEFAULT_EFFECT_POST = true;

// get post mode
boolean post = DEFAULT_EFFECT_POST;
if (effectType.isSetPost()) {
...</pre>
Wow! Clean, Readable, Changeable!</strong>

Good Code :)