Java Style Guidelines

Naming Conventions

Use the Java naming conventions

Submission Requirements

For submitting assignments:

Descriptive Names

Choose descriptive names. Avoid the following:

double t = 10.0; //does t stand for tip? for total?
int n = 2; //does n stand for number?
double mpg = 35; //good name!
boolean iaa = true; //bad name! is account active?
boolean eatMyShorts = true; //huh?
int puddle = 25; //wait, what?!?!

Spacing

Include white space above each method and logical section of code for readability.

Bad Style Good Style

 

Don't let a line of code stretch off-screen.

Follow the suggested placement for Class elements

public class ClassName
{
    //fields 

    //constructors

    //methods
}

Comments

Include descriptive comments throughout your code. Each logical section of code should have a comment above it.

Bad Style!

System.out.println("Please enter first name: ");
String firstName = scanner.nextLine();
System.out.println("Please enter last name: ");
String lastName = scanner.nextLine();

System.out.println(firstName + " " + lastName);

Good Style!

//get user inputs
System.out.println("Please enter first name: ");
String firstName = scanner.nextLine();
System.out.println("Please enter last name: ");
String lastName = scanner.nextLine();

//print the results
System.out.println(firstName + " " + lastName);

 

Comment Header

Every class should have a multi-line comment header at the very top of the document. The comment should include:

Example:
/*
 * Bob Smith
 * 9/19/2015 
 * my_filename.java
 * This file contains a class with some Java code!
 */

Curly Braces

Same-line style or next-line style are both OK. But pick one style and be consistent. All your braces should use the same format.

Same-line!

public Class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Next-line!

public Class Point 
{
    private int x;
    private int y;

    public Point(int x, int y) 
    {
        this.x = x;
        this.y = y;
    }
}

Constants

Do not leave unnamed numerical values floating around your code. These are called "magic numbers" and make your code harder to read and maintain. Replace them with named constants. The numbers 0, 1 and 2 are generally not considered magic numbers (unless they have special meaning in your problem set).

Bad Style!

Employee[] employees = new Employee[3];
for (int i = 0; i < employees.length; i++)
{
    employees[i] = createEmployee();
}

//did our first employee work overtime?
if (employees[0].getHours() > 40)
{
    System.out.println("Found one!");
}

Good Style!

final int NUM_EMPLOYEES = 3;
final int OVERTIME_THRESHOLD = 40;

Employee[] employees = new Employee[NUM_EMPLOYEES];
for (int i = 0; i < employees.length; i++)
{
    employees[i] = createEmployee();
}

//did our first employee work overtime?
if (employees[0].getHours() > OVERTIME_THRESHOLD)
{
    System.out.println("Found one!");
}

Redundancy

Don't ever, ever, ever repeat yourself in code. This is one of the core skills of a seasoned software developer. Use methods, loops and functions to remove redundancy in your code.

Bad Style!

Employee[] employees = getEmployees();

Employee first  = employees[0];
System.out.println(first.getName());
System.out.println(first.position());
first.giveRaise(0.10);

Employee second = employees[1];
System.out.println(second.getName());
System.out.println(second.position());
second.giveRaise(0.10);

Employee third = employees[2];
System.out.println(third.getName());
System.out.println(third.position());
third.giveRaise(0.10);

Good Style!

Employee[] employees = getEmployees();

for (int i = 0; i < employees.length; i++)
{
    Employee current = employees[i];
    System.out.println(current.getName());
    System.out.println(current.position());
    current.giveRaise(0.10);
}

 

Note: it can sometimes be very challenging removing redundancies in your programs. With more practice, removing repeated code becomes easier

Packages

Never leave classes in the default package. Create a new package to group together your classes under a namespace. The name of your package should follow the "reverse internet domain" style.

eg. edu.greenriver.it.mypackagename

Methods

All methods should be small and well-defined, and reusable. In general, methods shouldn't contain more than about 10 lines of code.

Javadocs

Each class should have full Javadocs. Your file should include:

Example:

/**
 * This class represents a person
 *
 * @author Sarah Smithers
 * @version 1.0
 */
public class Person
{
    private String name;
    private String nickname;

    /**
     * Creates a new Person with a name and nickname.
     *
     * @param name the name of the new person
     * @param nickname the nickname of the new person
     */
    public Person(String name, String nickname)
    {
        this.name = name;
        this.nickname = nickname;
    }

    /**
     * Gets the full name of the person.
     * @return the name and nickname of the person
     */
    public String getFullName()
    {
        return name + "(" + nickname + ")";
    }

    private void printName()
    {
        System.out.println(getFullName());
    }
}

Credit: Josh Archer, 2016