CPSC120A: Fundamentals of Computer Science
Fall 2011: Code Conventions
- Indentation:
- All lines should be 80 characters or less (the default width of
an Emacs window).
- When breaking a line that is longer than 80 characters
- break after commas.
- break before operators.
- prefer higher level breaks.
- Opening curly braces can be on the end of a line, or on the
next line indented the same as the above line (but they must
be consistent across an entire file).
- Closing curly braces should be indented the same as the line
that contains the opening curly brace it is paired with.
- All lines of code between an opening and closing curly brace
should be indented once more than the lines containing the opening
and closing curly braces.
- All lines of code of a case in a switch statement, should be
indented one more time than the line with the case.
- Comments:
- All Java files should have a block comment at the top with the
file name, author, and purpose of the file.
- All local variable declarations should have an end-of-line
comment describing the purpose of the variable.
- All methods should have a block comment above them with
a description of the purpose of the method.
- Declarations:
- There should be only one variable declaration per line
(unless multiple variables are related enough to warrant one
comment for all).
- Initialize variables on the same line as they are declared,
if possible.
- Declare variables at the beginning of the block of code
in which they will be used (a block of code is code
contained in curly braces).
- The exception to the above rule is with loops, never declare
a variable inside of a loop.
- Statements:
- Lines should not have more than one statement (a statement ends
with a semicolon or an opening curly brace).
- Control structures (if, else, for, while, etc.) should
always use curly braces.
- White Space:
- Blank lines should be used to separate related sections
of code and should be preceded by a comment explaining
the section of code.
- Blank line separated sections of code should not be more
than 20 lines.
- Variable declarations should be separated from subsequent code
with a blank line.
- Methods should be separated by blank lines.
- Binary operators (+, -, *, etc.) should be separated from
their operands by spaces (except for the . operator).
- Unary operators (-, ++, etc.) should not be separated from their
operand by a space.
- Opening curly braces that are not on their own line should be
separated from preceding code by a space.
- Argument lists should have spaces after commas.
- Control structures (if, for, while, etc.) should have a
space between before the opening parenthesis.
- The statements in a for loop should be separated with
spaces.
- Casts should be followed by a space.
- Naming Conventions:
- Variable names should be short and meaningful (common
abbreviations are OK (avg, max, etc.) but single character
names and acronyms should be avoided).
- Variable names should be in lower camel case where
words are joined without spaces and all words except the first
are capitalized.
- Constants should be in all uppercase with words separated
by underscores.
- Class names should describe what the class is for (Assignment1
does not describe the purpose of the class).
- Class names should be nouns in upper camel case (no spaces and
all words are capitalized).
- Methods should start with verbs and be in lower camel
case.
- Programming Practices:
- Use only one assignment operator per statement.
- Avoid magic numbers (literal primitive numbers) by declaring
local constants with meaningful names.
- Instance variables are for representing an object of a
class, not a convenient way to pass information between
methods.
- Instance variables should be private, unless constant, or
in method-less abstract class.
- Do not use the assignment operator in conditional and loop
statements except when needed in the for loop.
- Methods should have only one return statement (if any) and
it should be the last statement in the method.