There are many standards and best practices for Java Developers out there. This article outlines ten most basic rules that every developer must adhere to and the disastrous outcomes that can follow if these rules are not followed.
1. Add comments to your code. –Everybody knows this, but somehow forgets to follow it. How many times have you “forgotten” to add comments? It is true that the comments do not literally contribute to the functionality of a program. But time and time again you return to the code that you wrote two weeks ago and, for the life of you, you cannot remember what it does! You are lucky if this uncommented code is actually yours. In those cases something may spark your memory. Unfortunately most of the time it is somebody else’s, and many times the person is no longer with the company! There is a saying that goes “one hand washes the other.” So let’s be considerate to one another (and ourselves) and add comments to your code.
2. Do not complicate things. – I have done it before and I am sure all of you have. Developers tend to come up with complicated solutions for the simplest problems. We introduce EJBs into applications that have five users. We implement frameworks that an application just does not need. We add property files, object-oriented solutions, and threads to application that do not require such things. Why do we do it? Some of us just do not know any better, but some of us do it on purpose to learn something new, to make it interesting for ourselves. For those who do not know any better, I recommend reaching out to the more experienced programmers for advice. And to those of us that are willing to complicate the design of an application for personal gains, I suggest being more professional.
3. Keep in Mind – “Less is more” is not always better. – Code efficiency is a great thing, but in many situations writing less lines of code does not improve the efficiency of that code. Let me give you a “simple” example:
if(newStatusCode.equals(“SD”) && (sellOffDate == null ||
todayDate.compareTo(sellOffDate)<0 || (lastUsedDate != null &&
todayDate.compareTo(lastUsedDate)>0)) ||
(newStatusCode.equals(“OBS”) && (OBSDate == null ||
todayDate.compareTo(OBSDate)<0))){
newStatusCode = “NYP”;
}
How easy is it to figure out what this “if” condition is doing? Now imagine that whoever wrote this code, did not follow rule number 1 – Add comments to your code.
Wouldn’t it be much easier if we could separate this condition into two separate if statements? Now, consider this revised code:
if(newStatusCode.equals(“SD”) && (sellOffDate == null ||
todayDate.compareTo(sellOffDate)<0 || (lastUsedDate != null &&
todayDate.compareTo(lastUsedDate)>0))){
newStatusCode = “NYP”;
}else
if(newStatusCode.equals(“OBS”) && (OBSDate == null ||
todayDate.compareTo(OBSDate)<0))
{
newStatusCode = “NYP”;
}
Isn’t it much more readable? Yes, we have repeating statements. Yes, we have one extra “IF” and two extra curly braces, but the code is much more readable and understandable!
