Use the Java naming conventions
For submitting assignments:
Choose descriptive names. Avoid the following:
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
}
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: "); |
Good Style! //get user inputs |
Every class should have a multi-line comment header at the very top of the document. The comment should include:
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 { |
Next-line! public Class Point |
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]; |
Good Style! final int NUM_EMPLOYEES = 3; |
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(); |
Good Style! Employee[] employees = getEmployees(); |
Note: it can sometimes be very challenging removing redundancies in your programs. With more practice, removing repeated code becomes easier
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
All methods should be small and well-defined, and reusable. In general, methods shouldn't contain more than about 10 lines of code.
Each class should have full Javadocs. Your file should include:
/**
* 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