Java Programming Style Guide

White Space

Use blank lines and blank spaces to improve the readability of your code.

Use blank lines to separate chunks of program code. Chunks are logical groups of program statements (generally 3 to 7 lines in length) and usually proceeded with a single-line summary comment. Use one blank line before every program chunk. Use two blank lines before the start of each new method within a class.

Use one blank space on both sides of operator symbols, after commas in argument lists, and after semicolons in for statements.

Rationale

Blank lines and spaces allow the programmer to show paragraphing or organization of the code into small, logical groupings to improve readability. Look at the two examples below and notice how much more readable the lower example is compared to the upper example without blank lines and spacing.

    //Prompt the user to enter the commission rate
    SimpleIO.prompt("Enter commission rate (as a percentage): ");
    userInput=SimpleIO.readLine();
    double commissionRate=Double.parseDouble(userInput)/100;
    //Compute and display the commission, rounding to the nearest cent
    double commission=totalValue*commissionRate;
    commission=Math.round(commission*100)/100.0;
    System.out.println("Commission:$"+commission);
   
    // Prompt the user to enter the commission rate
    SimpleIO.prompt("Enter commission rate (as a percentage): ");
    userInput = SimpleIO.readLine();
    double commissionRate = Double.parseDouble(userInput) / 100;

    // Compute and display the commission, rounding to the nearest cent
    double commission = totalValue * commissionRate;
    commission = Math.round(commission * 100) / 100.0;
    System.out.println("Commission: $" + commission);