Java Best Coding Practices

In every programming language, there is a list of best coding practices and Java is no different. While some practices are non-negotiable, others are more based on the personal style of the individual programmer. When it comes to Java, there are some practices that I follower as a Java developer. The list below is by no means complete, but it does touch on some key areas of programming practices.

Class Names

I don’t like having a small amount of large classes so I end up sticking with smaller classes but more of them. Due to the number of classes I end up with, I use class names that are very specific and are easily identifiable. Of the two Java classes below, which one is easier to identify as to its purpose?

public class Class1 
{
    // do something
}

public class ConfigurationLoader
{
   //do something
}


Method Names

I keep the same specific naming convention on the method names as I do for the class names. The String1 method name is way too generic and not good coding. The second example is very good because it identifies exactly what the Java method does.

public int count(String str)
{
   //do some counting
}

public int countOccurrences(String inputString)
{
   //do some counting
}


Variable Names

Since there are so many variables contained within a Java class, I make the variable names as descriptive as possible, even though some names get pretty long. I want to look at a Java variable and know what data it’s holding. The String variable “string1” is too generic, but the String “firstName” tells me exactly what it is holding. You can easily spot the better integer variable name below.

public String string1;
public String firstName;
----------------------------
int i = 0;
int loopCounter = 0;


Programming Style

I think programming style is something specific to the individual Java programmer. Here is a list of areas where individual programming style will be different:

  • Where to put curly braces?
  • Multiple method calls on same line?
private void incrementCounter()
{
   //increment counter
}
...........OR............
private void incrementCounter() {
   //increment counter
}
-----------------------------------------
String myTestString = "HELLO";
System.out.println(myTestString.toLowerCase().length());


Code Comments

I am a big fan of putting comments in the Java code I write. I like to put a comment at the top of a method as well as any line in the code where it is not clear what is going on. The three examples below are how I like to add comments to Java code.

//Method inputs a String and returns an integer
public int getHitCounter(String someIdentifier)
{
    int hitCounter = o;
    //do something
    return hitCounter;
}
-----------------------------------------
//String to hold customer last name
private String lastName = "";
-----------------------------------------
//Store all environment variables in Java Map
static Map<String,String> systemEnv = System.getenv();


Your Input Best Coding Practices?

Let us know your style of Java programming. You like to do this or you never do that. Let’s hear it because Seymour wants to know!

One Comment

Add a Comment

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