10 Commandments for Java Developers

  • Published on

  • View

  • Download


Commandments for Java Developers


<ul><li><p>10 Commandments for Java Developers</p><p>By Aleksey Shevchenko</p><p>There are many standards and best practices for Java Developers out there. Thisarticle outlines ten most basic rules that every developer must adhere to and thedisastrous outcomes that can follow if these rules are not followed.</p><p>1. Add comments to your code. Everybody knows this, but somehow forgets tofollow it. How many times have you "forgotten" to add comments? It is true that thecomments do not literally contribute to the functionality of a program. But time andtime again you return to the code that you wrote two weeks ago and, for the life ofyou, you cannot remember what it does! You are lucky if this uncommented code isactually yours. In those cases something may spark your memory. Unfortunatelymost of the time it is somebody else's, and many times the person is no longer withthe company! There is a saying that goes "one hand washes the other." So let's beconsiderate to one another (and ourselves) and add comments to your code.</p><p>2. Do not complicate things. I have done it before and I am sure all of youhave. Developers tend to come up with complicated solutions for the simplestproblems. We introduce EJBs into applications that have five users. We implementframeworks that an application just does not need. We add property files, object-oriented solutions, and threads to application that do not require such things. Whydo we do it? Some of us just do not know any better, but some of us do it onpurpose to learn something new, to make it interesting for ourselves. For those whodo not know any better, I recommend reaching out to the more experiencedprogrammers for advice. And to those of us that are willing to complicate the designof an application for personal gains, I suggest being more professional.</p><p>3. Keep in Mind "Less is more" is not always better. Code efficiency is agreat thing, but in many situations writing less lines of code does not improve theefficiency of that code. Let me give you a "simple" example:</p><p>if(newStatusCode.equals("SD") &amp;&amp; (sellOffDate == null ||todayDate.compareTo(sellOffDate)0)) ||(newStatusCode.equals("OBS") &amp;&amp; (OBSDate == null ||todayDate.compareTo(OBSDate)</p></li><li>newStatusCode = "NYP";}else if(newStatusCode.equals("OBS") &amp;&amp; (OBSDate == null ||todayDate.compareTo(OBSDate)</li><li><p>6. Say no to Print lines and String Concatenations. I know that for debuggingpurposes, developers like to add System.out.println everywhere we see fit. Andwe say to ourselves that we will delete these later. But we often forget to deletethese lines of code or we do not want to delete them. We use System.out.printlnto test, why would we be touching the code after we have tested it? We mightremove a line of code that we actually need! Just so that you do not underestimatethe damage of System.out.println, consider the following code:</p><p>public class BadCode{</p><p>public static void calculationWithPrint(){</p><p>double someValue = 0D;for (int i = 0; i &lt; 10000; i++){</p><p>System.out.println(someValue = someValue + i);}</p><p>}public static void calculationWithOutPrint(){</p><p>double someValue = 0D;for (int i = 0; i &lt; 10000; i++){</p><p>someValue = someValue + i;}</p><p>}public static void main(String [] n){</p><p>BadCode.calculationWithPrint();BadCode.calculationWithOutPrint();</p><p>}}</p><p>In the figure below, you can observe that method calculationWithOutPrint()takes 0.001204 seconds to run. In comparison, it takes a staggering 10.52 secondsto run the calculationWithPrint() method.</p><p>The best way to avoid such CPU waste is to introduce a wrapper method that lookssomething like this:</p><p>public class BadCode{</p><p>public static final int DEBUG_MODE = 1;public static final int PRODUCTION_MODE = 2;</p><p>public static void calculationWithPrint(int logMode){</p><p>PDF Creator - PDF4Free v2.0 http://www.pdf4free.com</p></li><li><p>double someValue = 0D;for (int i = 0; i &lt; 10000; i++){</p><p>someValue = someValue + i;myPrintMethod(logMode, someValue);</p><p>}}</p><p>public static void myPrintMethod(int logMode, double value){</p><p>if (logMode &gt; BadCode.DEBUG_MODE){</p><p>return;}</p><p>System.out.println(value);}</p><p>public static void main(String [] n){</p><p>BadCode.calculationWithPrint(BadCode.PRODUCTION_MODE);}</p><p>}</p><p>String concatenation is another CPU waster. Consider example below:</p><p>public static void concatenateStrings(String startingString){</p><p>for (int i = 0; i &lt; 20; i++){</p><p>startingString = startingString + startingString;}</p><p>}</p><p>public static void concatenateStringsUsingStringBuffer(String startingString)</p><p>{StringBuffer sb = new StringBuffer();sb.append(startingString);</p><p>for (int i = 0; i &lt; 20; i++){</p><p>sb.append(sb.toString());}</p><p>}</p><p>In the following figure you can see that the method that uses StringBuffer takes.01 seconds to execute where as the methods that use string concatenation takes.08 seconds to execute. The choice is obvious.</p><p>PDF Creator - PDF4Free v2.0 http://www.pdf4free.com</p></li><li><p>7. Pay attention to the GUI. No matter how absurd it sounds; I repeatedlyobserve that GUI is as important to the business clients as functionality andperformance. The GUI is an essential part of a successful application. Very often ITmanagement tends to overlook the importance of GUI. Many organizations savemoney by not hiring web designers who have experience in design of "user-friendly"applications. Java developers have to rely on their own HTML skills and their limitedknowledge in this area. I have seen too many applications that are "computerfriendly" rather then "user friendly". Very rarely I have seen developers that areproficient in both software development and GUI development. If you are thisunlucky Java developer who has been assigned to create an application interface,you should follow these three rules:</p><p>1. Do not reinvent the wheel. Look for existing applications that have similarinterface requirements.</p><p>2. Create a prototype first. This is a very important step. The clients like to seewhat they are going to get. It is better for you also because you are going toget their input before you go all out and create an application interface thatwill leave the clients cold.</p><p>3. Put the user's hat on. In other words, inspect the application requirementsfrom the user's perspective. For example, a summary screen can be createdwith paging and without. As a software developer, it might be temping for youto omit paging from the application because it is so much less complicated.But, from the client's perspective, it might not be the best solution becausethe summary results can hold hundreds of rows of data.</p><p>8. Always Prepare Document Requirements. Every business requirement mustbe documented. This could be true in some fairy tale, but it is far from that in thereal world. No matter how time-pressed your development is, no matter how tightthe deadlines, you must always make sure that every business requirement isdocumented.</p><p>9. Unit-test. Unit-test. Unit-test. I am not going to go into any details as towhat is the best way to unit-test your code. I am just going to say that that it mustbe done. This is the most basic rule of programming. This is one rule that, above all,cannot be omitted. It would be great if your fellow developer could create andexecute a test plan for your code, but if that is not possible, you must do it yourself.When creating a unit test plan, follow these basic rules:</p><p>1. Write the unit test before writing code for class it tests.2. Capture code comments in unit tests.3. Test all the public methods that perform an "interesting" function (that is, not</p><p>getters and setters, unless they do their getting and setting in some uniqueway).</p><p>10. Remember quality, not quantity. - Do not stay late (when you do not haveto). I understand that sometimes production problems, urgent deadlines, andunexpected events might prevent us from leaving work on time. But, managers donot appreciate and reward their employees because they stay late on regular basis,they appreciate them because they do quality work. If you follow the rules that Ioutline above, you will find yourself producing less buggy and more maintainablecode. That is the most important part of your job.</p><p>PDF Creator - PDF4Free v2.0 http://www.pdf4free.com</p></li></ul>