Simple Style Rules for Java

 

Style entails a program's readability and logic structuring. Style is almost as important as correctness in programming. This is useful for you, the programmer, when you are writing code and us, the graders for reading and making sense of a program. Also, in industry and real-world situations, you usually do not write a whole program at once, nor do you only look at programs you wrote. Usually industry programmers are looking at code they did not write or wrote a long time ago, which is where style and comments become very important to understanding programs.

 

Good rules to live by (especially if you don't want to lose points on your programs):

 

1. Capitalize class names

 

public class Thing

 

2. Do NOT capitalize the first letter of methods or variables

 

public void methodName()

{ }

 

int num1 = 3;

 

Notice: In the above example, interior letters of method names and variables can be capitalized. Usually when two or more words are combined into one method name or variable name, we capitalize the additional words i.e. methodName. This is sometimes referred to as camelBack.

 

3. Write comments!

 

Comments do not have to be excessive; normally they won't be on every line of a program. However, there should at least be a comment explaining the whole class, comments for each method, comments for the declaration of variables, and comments for confusing or long parts of the code.

 

//*******************************************

// include comment boxes

//*******************************************

 

// and include comments inside the code for tricky parts

 

4. Use meaningful variable names

 

int numTokens;

 

double hypotenuse;

 

DON'T use double h; or int nT - these are too vague. Also, don't use 10 words in 1 variable - that's a little too much.

 

5. Use constants for constant values

 

Ex: Above, if you are calculating prices of items, the texasTaxRate will be useful. However, this doesn't change frequently - it is constant. So, in Java you should declare this constant like:

 

final double TRAVIS_PROPERTY_TAX_RATE = 0.014;

 

Similarly, if you are using any real world value that is constant, like how much quarters are worth, or what gravity is, declare it final in your program.

 

Notice: I used all capital letters for the constant, separating words with underscores. This is typical Java convention.

 

6. Indent!

 

When you declare a class or a method, indent at least 3 spaces to start the next line. All method declarations should line up with each other. Also, when you have an if-statement or a loop-statement, you should always indent the body at least 3 spaces.  You should be fine if you follow any of the indention patterns from your textbook.  The key point is to be consistent!

 

public class Thing

{

 

   public static void main(String[] args)

   {

 

      int num = 3;

 

      if (num > 0)

         if (num < 10)

            num++;

                    else

            num--;

      else

         num = 0;

 

   } //main

 

 }//Thing

 

This is an edited version of a document prepared by Dr. Roger Priebe.  Thanks, Roger.