Java Code Conventions

When writing Java code you must follow the Code Conventions for the Java Programming Language. It’s a document written by Sun back in 1997. Why should you read such an ancient (in computer science terms) document?

Code conventions are important to programmers for a number of reasons:

* 80% of the lifetime cost of a piece of software goes to maintenance.
* Hardly any software is maintained for its whole life by the original author.
* Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

Some people tell me that they cannot change their style, because that’s how they are used to coding. Fair enough. Do whatever you want when coding alone, in your home. But not in a professional environment. If you can’t be bothered, do us a favour and leave. Resign. Start selling popcorn. Whatever.

I have a serious problem working with people who commit Java code which looks like this:

public class persons {
public void PersonSave(persons p) {
package foo.bar.personUtils;
public static final String foo = "whatever";
whatever()
{
  // do stuff
}

Seriously, please try to read the following piece of code found in a real life project. Does this look like Java?

if(e<0.0)d= -d;
if(d!=0.0)for(int i=0;i<dim;i++)this.n[i] = this.n[i]/d;
else this.tW.writeString("normalise: non simplex");
Object leftList = null, rightList = null;
try{ leftList = c.newInstance();}
catch(Exception e){tW.writeString("sort:error1 ");return null;};

OK, this style might have been good in an Obfuscated Code Contest, but it definitely does not get you going in the workplace.

Good luck

4 Responses to “Java Code Conventions”

  1. akz Says:

    Congrats! Gettin’ jiki with it!!!!

  2. dimzava Says:

    Happy blogging Yannis.
    I totally agree with you about code conventions. But you can find some package names with upper case letters, even in the Java API specification. Legacy I guess?

  3. cherouvim Says:

    There are many conventions violated in the JDK. The point is to trying to adhere to those so you can write cleaner and more maintainable code.

  4. Spiros Tzavellas Says:

    Welcome to blogshere Yannis!

    I hope that you will share your thoughts with us regularly.

    Regarding your post I just wanted to add that the best programmers I’ve worked with are very pedantic and always have and follow coding conventions.