Coding Standards
- Comments
- Start all
programs with a comment block. Use Javadoc style comment blocks (i.e.,
begin with /** and end with */).
At a minimum, include the following information:
i. A brief description of what your
class/program does.
ii. Your name
iii. The date
iv. Class and section number
v. Assignment/Project number or name
vi. Example:
/**
* This is an example comment block. Program
* description goes here.
*
* Name: Mike Jochen
* Date: 12/31/99
* CPSC 123 A456
* Project 1
**/
- Describe
every method with a comment block placed immediately before the method. Again,
use Javadoc style comment blocks. The comment should give a brief
description of the method, including input and output.
- Inside
each method, use comments to describe what you are doing at major sections.
- You can
use comments to further describe variables, but start with a good
variable name!
- Don’t go
overboard with comments. Use them in places where you need to describe
what is happening in your program, or to separate major sections of code.
Do not comment every line of source code!
- Class names
- Use a name
that describes your class.
- Start
class names with an upper-case letter.
- Class
names should be a noun (e.g., car, person, book, factory, etc.)
- If using
more than one word as a class name, run the words together and capitalize
the first letter of each word.
Example:
CarFactory (note: this is often referred to as
camel notation.)
- Method
names
- Use a name
that describes what your method does.
- Start
method names with a lower-case letter.
- If using
more than on word as a method name, run the words together and capitalize
the first letter of each word after the first. Example: wordSort.
- Variable
names
- Use a name
that describes you variable – what information it holds.
- Start
variable names with a lower-case letter.
- If using
more than on word as a variable name, run the words together and
capitalize the first letter of each word after the first. Example: radiusInput.
- Constants
- Don’t
use “magic numbers” in your program (i.e., a value that just appears in your source
code). If you need to use a constant value, declare a constant and use
the constant name instead of the value.
Example: public final
double PI = 3.14159
- Use
descriptive names for all constant values
- Names for
constant values should be in all capitals.
- Use
underscores to separate words in constants.
Example: public final
double PAY_RATE = 10.75
- Indent
- Indent
each nested block of code.
- The
specific amount of indent is not as important as being consistent. A
common amount of indent is 4 spaces. Most IDEs/editors have configured
the tab key as a good indent amount (or will indent automatically for
you!). Use the same indent amount for each nested block of code!
- Example:
if ( x < 10) {
System.out.println(“X is too small”);
if (x < 0) {
System.out.println(“X is REALLY too small”);
x = 10 * y;
}
} else {
System.out.println(“X is just right.”);
}
- Performance
tricks versus readable code
- We all
love to make our code as efficient as possible, trying to optimize for space,
or speed, or some other metric. However, writing optimized code often makes
for code that is more difficult to understand (performance optimization
and program comprehension are usually conflicting goals)
- In this
class, program comprehension wins out over the use of arcane language
constructs or other features that may allow you to do more things in less
time or space.
- Brace
placement
- Be
consistent in how/where you place your braces around blocks of code
- One common
scheme is to place braces at the end of the line as in the example given
above.
- Another
scheme is to place braces on a new line, by themselves.
- I don’t
care which scheme you use, but be consistent!
- The overall
goal is to make sure your code is neat, READABLE, and UNDERSTANDABLE. Code
that is difficult to read usually results in code that is difficult to
understand. That’s when errors magically appear! Your code should be
self-documenting (i.e., it should explain what it is doing inline). Anyone
should be able to pick up your code and be able to understand what is going
on.