CPSC 120B: Fundamentals of Computer Science I
Fall 2009: Code Conventions
- Classes
- Class names should be descriptive
- Class names should nouns with the first letter of each word capitalized (but with no spaces) and be descriptive
- Comments:
- Java files should contain a comment at the top with the file name, author, and purpose of the file
- Methods should have a comment at the top with a description of the purpose of the method
- Each variable declaration should have an end of line comment describing the purpose of the variable
- Formatting:
- Opening curly braces can be on the end of a line, or on the next line indented the same as the above line
- 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 inbetween an opening and closing curly brace should be indented one 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
- All lines should be 80 characters or less
- Sections:
- Code should be separated into small logical sections using blank lines
- Sections of code should have a comment above them that describes the purpose of the sections
- Sections of code should not be longer than 20 lines (and should probably be shorter)
- Variables:
- Variable names should be descriptive
- Variable names should begin with a lower case letter with the each subsiquent word capitalized (but with no spaces)
- Constant variable names should be in all capital letters with each word separated with an underscore character
- All variabes should be declared at the beginning of a block of code (a block of code is contained in curly braces)
- Spacing:
- Binary operators (+, -, *, /, %) should have spaces in between them and their operands.
- Except for the the . operator, it should not have spaces on either side
- Unary operators (-, ++, --) should not have spaces in between them and their operands
- Except for cast operators, they should have a space after them
- There should be a space between conditional operators (if, else, switch) and the opening parenthesis
- If opening curly braces are not alone on a line there should be a space between it and the rest of the code on the line
- Other:
- Avoid using literal values (use constants instead)
- All if and else statements should use curly braces