Java Style Guide

From Studyplace

Jump to: navigation, search

The style you use to write your program code makes it more easily readable by you and others. Style generally includes the use of indentation and other whitespace and naming conventions (for classes, functions, variables, parameters, ...). While the best coding style (braces/indents) is hotly debated, it is generally recognized that all code within one project should have the same style.

This very short style guide leaves much unsaid, relying on the common sense of the programmer.

Contents

[edit] Indentation

  • save tabs as spaces (there should be no tab characters in the file)
  • a tab will be 4 spaces
  • all curly braces will be on a new line, with the code block indented one 4 spaces

public void printNumbers(int start, int end)
{
    for(int i = start; i<=end; i++)
    {
        System.out.println("num: " + i);
    }
}

[edit] Text Encoding

Plain text is not always so plain. Set your editor to save files as UTF-8.

[edit] New Lines

New lines should be 'unix style', i.e. \n as the new line marker.

[edit] Names

All names should be descriptive. Long names are fine -- it is better that they make sense than be concise. Do not use any suffixes or prefixes to include additional information (no hungarian, please). Your clear name and the context in which it is used should suffice.

Specifically:

  • all non-static variables and methods use camel case: thisIsAVariable not this_is_a_variable
  • all class and interface names should start with capital: Person
  • all non-static variable and all function names should start with lower case: readFromFile() or firstName
  • all static variables upper-case: MAXIMUM_USERS
  • only the first letter of abbreviations should be capitalized: openUrl() not </tt>openURL()</tt>
  • package names should be all lowercase, with no spaces between words: edu.tc.mstu5031.webtools not edu.tc.mstu5031.webTools
  • class names and variable names should be singular, unless the represent more than one of the object: public class Cat not public class Cats


Full example:

package edu.tc.mstu5031.user;

public class Person
{

    private static final MIN_DRINKING_AGE = 21;
    private static final MIN_VOTING_AGE = 18;

    private int age = 0;
    private String firstName = "";
    private String lastName = "";


    public Person()
    {
        super();
    }

    public Person(String firstName, String lastName, int age)
    {
        this.age = age;
        this.firstName = firstName;
        this.lastName = lastName;
    }


    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getFirstName() { return first name; }

    public void setLastName(String lastName) { this.lastName = lastName; }
    public String getLastName() { return lastName; }

    public void setAge(int age) { this.age = age; }
    public int getAge() { return this.age; }


    public boolean canDrink()
    {
        return age >= MIN_DRINKING_AGE;
    }

    public boolean canVote()
    {
        return age >= MIN_VOTING_AGE;
    }

}
Personal tools