Coding Style Summary
Why use a coding style?
This page summarizes the
diffferent coding style guidelines used throughout this course. More may be added.
I highly recommend setting up your preferences in your IDE to follow these guidelines.
Note: I expect you to learn and follow these coding style guidelines for
all work turned in for this class. If you do not understand the reasons for
a particular rule, ask!
- Your program should all have the import statements first,
at the top of the file.
- The comment block at the top of your file that explains the program
will be a javadoc comment that includes @author followed by your name.
It will include a description that explains your program, like this:
/**
* This comment at the top explains the class or program.
*
* @author Your Name
*
* Andrew ID: yourid
*
* Don't forget to add the Academic Integrity statement!
*
*/
- The stars (asterisks) will line up and the comment will be formatted to
be very readable.
- After the comment is the start of your code.
- The structure of your code will accurately reflect the flow of
execution for your program.
- Curly braces go on a line by themselves and they line up with the
surrounding code. Note: I will enforce this if there are any issues with your readability.
- Each statement within a block of code (inside the curly braces) is
indented by 4 spaces - map your tab to 4 spaces.
Code with tabs will not be accepted and
you will earn a 0 for that program.
The one exception to the indentation is the first set of curly braces -
you may chose to NOT indent the first block of code code within the class.
The following code demonstrates the proper basic indentation:
public class HiWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
- A method should be designed to be re-usable and to match the way
people think as much as possible.
- Method names and parameter names should make very clear what the method
does and what the parameters represent.
- A method with nothing to return should have a return type of void.
- Every method should have a javadoc comment block immediately
preceding the signature that describes the method in a single sentence.
- The comment should not include information that can be determined
by reading the method's signature.
- Declarations should go right after a method's signature, inside
the open curly brace.
- Identifiers follow the camel-back naming convention, which means that
individual words that make up the identifier are capitalized.
- Identifiers that are names of objects, including local parameters,
begin with a lower case letter.
- Identifiers that are names of classes begin with an upper case letter.
- Statements that are long should be broken up into multiple lines to
keep the code readable.
- Rather than relying on the rules of precedence,
parentheses should be used in arithmetic expressions
to make the meaning of your code clear.
- All operators should be surreounded by whitespace. For instance, this is correct:
a = 12 * (var1 / xyz);
This is not correct:
a=12*(var1/xyz);
- The ++ and -- operators should be used only in separate statements or
in for loops.
- A datafile name should not include a drive letter.
- As soon as your code is finished with a datafile, it will close
the file.
- The instance variables in a helper class should be private.
- Private instance variables should be accessed through public
access methods (get-methods).
- Since nouns become instance variables, instance variable names
should be nouns, or start with "is" if they represent a state.
- Access method names should start with "get", unless they correspond to
a state. An access method that returns a state should start with "is".
- Methods that set an instance variable to a value passed in as an
argument should have names that start with "set" (set-methods).
- Since verbs become methods, method names should be verbs.
- There are two different ways to
declare an array and both ways demonstrate good coding sytle. You can
put the square brackets before the variable name or after the variable
name:
int [] array1;
int array1[];
Whichever
style you use, the most important guideline to follow is to be consistant.
So, either put the brackets before the variable name or after it, but do
not mix the two styles.
- Input loops will follow the basic structure for a while loop that
gets input:
Get Input
while (Check Input)
{
Process
Get Input
}
Here are more details and additional coding examples that demonstrate
proper indentation:
- Try/catch blocks will be indented as follows:
try
{
age = Integer.parseInt(inputAge);
}
catch (NumberFormatException e1)
{
System.out.println("*** Invalid age: " + inputAge);
age = -1;
}
- When writing an if or if-else statement, the if and
else parts are not indented, but the statements within each are indented
with 4 spaces. For instance, this is correct:
if (logical-expression)
statement
else
statement
- When writing an if or if-else statement, if one of the statements
is a compound statement, every statement (whether compound or not) will
be enclosed in curly braces. For instance, this is correct:
if (logical-expression)
{
statement
statement
}
else
{
statement
}
- The curly braces in a compound statement will not be indented, but
will line up with the if or the else part (see the above example).
- When writing nested if-else statements, the indentation will be as
follows:
if (logical-expression)
statement
else if (logical-expression)
statement
else
statement
- The code for the loop will not be indented and the code inside the
loop will be indented with 4 spaces.
For instance, these are all correct:
for (i = 0; i < 12; i++)
statement;
while (logical-expression)
statement;
do
statement;
while (logical-expression);
- If the statement inside the while loop is a compound statement, the
curly braces are not indented. For instance, these are all correct:
for (i = 0; i < 12; i++)
{
statement;
statement;
}
while (logical-expression)
{
statement;
statement;
}
do
{
statement;
statement;
}
while (logical-expression);