Coding Style Summary

Why use a coding style?

This page summarizes the diffferent coding style guidelines used throughout this course. More may be added.

I highly recommend setting up your preferences in your IDE to follow these guidelines.

Note: I expect you to learn and follow these coding style guidelines for all work turned in for this class. If you do not understand the reasons for a particular rule, ask!

  1. Your program should all have the import statements first, at the top of the file.

  2. The comment block at the top of your file that explains the program will be a javadoc comment that includes @author followed by your name. It will include a description that explains your program, like this:
    
    /**
     * This comment at the top explains the class or program.
     *  
     * @author Your Name
     *  
     * Andrew ID: yourid
     *
     * Don't forget to add the Academic Integrity statement!
     *  
     */
    

  3. The stars (asterisks) will line up and the comment will be formatted to be very readable.

  4. After the comment is the start of your code.

  5. The structure of your code will accurately reflect the flow of execution for your program.

  6. Curly braces go on a line by themselves and they line up with the surrounding code. Note: I will enforce this if there are any issues with your readability.

  7. Each statement within a block of code (inside the curly braces) is indented by 4 spaces - map your tab to 4 spaces.

    Code with tabs will not be accepted and you will earn a 0 for that program.

    The one exception to the indentation is the first set of curly braces - you may chose to NOT indent the first block of code code within the class.

    The following code demonstrates the proper basic indentation:

    public class HiWorld 
    {
    public static void main(String args[])
    {
        System.out.println("Hello World!");
    }
    }
    

  8. A method should be designed to be re-usable and to match the way people think as much as possible.

  9. Method names and parameter names should make very clear what the method does and what the parameters represent.

  10. A method with nothing to return should have a return type of void.

  11. Every method should have a javadoc comment block immediately preceding the signature that describes the method in a single sentence.

  12. The comment should not include information that can be determined by reading the method's signature.

  13. Declarations should go right after a method's signature, inside the open curly brace.

  14. Identifiers follow the camel-back naming convention, which means that individual words that make up the identifier are capitalized.

  15. Identifiers that are names of objects, including local parameters, begin with a lower case letter.

  16. Identifiers that are names of classes begin with an upper case letter.

  17. Statements that are long should be broken up into multiple lines to keep the code readable.

  18. Rather than relying on the rules of precedence, parentheses should be used in arithmetic expressions to make the meaning of your code clear.

  19. All operators should be surreounded by whitespace. For instance, this is correct:
    a = 12 * (var1 / xyz);
    This is not correct:
    a=12*(var1/xyz);

  20. The ++ and -- operators should be used only in separate statements or in for loops.

  21. A datafile name should not include a drive letter.

  22. As soon as your code is finished with a datafile, it will close the file.

  23. The instance variables in a helper class should be private.

  24. Private instance variables should be accessed through public access methods (get-methods).

  25. Since nouns become instance variables, instance variable names should be nouns, or start with "is" if they represent a state.

  26. Access method names should start with "get", unless they correspond to a state. An access method that returns a state should start with "is".

  27. Methods that set an instance variable to a value passed in as an argument should have names that start with "set" (set-methods).

  28. Since verbs become methods, method names should be verbs.

  29. There are two different ways to declare an array and both ways demonstrate good coding sytle. You can put the square brackets before the variable name or after the variable name:
        int [] array1;
        int array1[];

    Whichever style you use, the most important guideline to follow is to be consistant. So, either put the brackets before the variable name or after it, but do not mix the two styles.

  30. Input loops will follow the basic structure for a while loop that gets input:
    Get Input
    while (Check Input)
    {
        Process
        Get Input
    }
    

Here are more details and additional coding examples that demonstrate proper indentation:

  1. Try/catch blocks will be indented as follows:
    try
    {
        age = Integer.parseInt(inputAge);
    }
    catch (NumberFormatException e1)
    {
        System.out.println("*** Invalid age: " + inputAge);
        age = -1;
    }
    

  2. When writing an if or if-else statement, the if and else parts are not indented, but the statements within each are indented with 4 spaces. For instance, this is correct:
    if (logical-expression)
        statement
    else 
        statement
    
  3. When writing an if or if-else statement, if one of the statements is a compound statement, every statement (whether compound or not) will be enclosed in curly braces. For instance, this is correct:
    if (logical-expression)
    {
        statement
        statement
    }
    else
    {
        statement
    }
    
  4. The curly braces in a compound statement will not be indented, but will line up with the if or the else part (see the above example).

  5. When writing nested if-else statements, the indentation will be as follows:
    if (logical-expression)
        statement
    else if (logical-expression)
        statement
    else 
        statement
    

  6. The code for the loop will not be indented and the code inside the loop will be indented with 4 spaces. For instance, these are all correct:
    for (i = 0; i < 12; i++)
        statement;
    
    while (logical-expression)
        statement;
    
    do
        statement;
    while (logical-expression);
    

  7. If the statement inside the while loop is a compound statement, the curly braces are not indented. For instance, these are all correct:
    for (i = 0; i < 12; i++)
    {
        statement;
        statement;
    }
    
    while (logical-expression)
    {
        statement;
        statement;
    }
    
    do
    {
        statement;
        statement;
    }
    while (logical-expression);