CSC 212: Programming with Data Structures

Spring 2016

Java Style Guide

Style guide for this course. These guidelines are not meant to be set in stone, but will help make your code more readable (both by yourself and others).

  • CamelCaseForEverything

  • Classes should start with a capital letter. Methods, fields, and variables should start with a lowercase letter. Examples:
    AddTax   // class
    addTax() // method
    taxRate  // field or variable
    

  • 80 characters per line

  • Javadoc comments for classes, fields, and methods:
    /** 
     * Description of what the program does
     *
     * @author Sara Sheehan
     * @version February 22, 2016
     */
    

  • Regular comments inside methods, for example:
    // insert each element one at a time
    

  • Whitespace: when in doubt, include a blank line, especially to separate loops and other blocks of code.

  • Each imported class called separately, for example:
    import java.util.Scanner;
    import java.util.Random;
      
  • Full English names for variables, methods, and classes

    • Example: userWords instead of w
    • Exception: some numbers (i.e. integer n, iterator i)

  • Indentation (with tabs) should match curly braces. Indentation has no effect on the code (unlike Python), but this helps keep it readable. Example:
    for (int i=0; i < myArray.length; i++) {
        System.out.println(myArray[i]);
    }
    
    // or
    
    for (int i=0; i < myArray.length; i++)
    {
        System.out.println(myArray[i]);
    }
    

  • Use this whenever referring to a field, for example:
    public class Person {
    
        private String name;
    
        public Person(String myName) {
            this.name = myName;
        }
    
        public String getName() {
            return this.name;
        }
    }