Code Conventions - Tonga Institute of Higher Education

Download Report

Transcript Code Conventions - Tonga Institute of Higher Education

Code Conventions
Tonga Institute of Higher
Education
What are Code Conventions?


Code Conventions are guidelines on how to
write code.
They are rules for
 File
Organization
 Code Width
 Indentation
 Declarations
 Statements
 White Space
 Naming Conventions
 Etc.
Why Have Code Conventions
Code
conventions are important to programmers for a
number of reasons:
1.Code
conventions improve the readability of the software,
allowing engineers to understand new code more quickly and
thoroughly.
2.80% of the lifetime cost of a piece of software goes to
maintenance. Hardly any software is maintained for its whole life
by the original author.
3.If you ship your source code as a product, you need to make sure
it is as well packaged and clean as any other product you create.
For code conventions to work, every person writing
software must conform to the code conventions. Everyone.
What this means to you
All homework must follow code
conventions.
 Points will be deducted if you do not follow
code conventions.
 I will not look at any code emailed to me
for help that does not follow code
conventions.

File Organization

Source Code should be written in the following
order:
1.
2.
Comment flowerbox.
Package Statements

3.
Import statements

4.
5.
6.
7.
package java.awt;
import java.awt.peer.CanvasPeer;
Class or Interface Statement
Variables
Constructors
Methods
Code Width


Avoid lines longer than 80 characters, since
they're not handled well by many terminals and
tools.
When an expression will not fit on a single line,
break it according to these general principles:
 Break
after a comma.
 Break before an operator.
 Tab the subsequent line once.
 For IF conditionals, tab the subsequent line twice.
Break after a comma
Break after
comma
Subsequent lines
use 1 tab
Break before an operator
Break before
An operator
Subsequent lines
use 1 tab
Operator is first
part of line
Indent all code within brackets

This will help you debug your code
Easy to see
what is done
In a class
Easy to see
what is done
In a method
Easy to see
what is done in
the body of the If
statement
For IF conditionals, tab the
subsequent line twice.
Two
tabs
Declarations

One declaration per line is recommended since it encourages
commenting
int level; // indentation level
int size; // size of table
is better than
int level, size;

Try to initialize variables where they're declared. The only reason
not to initialize a variable where it's declared is if the initial value
depends on some computation occurring first.
Blanks Spaces and Lines

Blank Spaces

A blank space should separate keywords such as if, for, etc.


x = 5 + 3;
someClass.someMethod(x, y, z);
A blank space should separate brackets


Example:
A blank space should appear after commas in argument lists.


if (x == y) {
All binary operators (+, -, <, ==, etc.) should be separated by spaces.


Example:
Example:
if (x == y) {
Blank Lines


A blank line should separate methods
A blank line should separate variables from other code
 Between logical sections inside a method to improve readability.

In other words, put blank lines where it makes sense to put blank lines
Naming Conventions - 1

Packages



All lowercase
One word
Example:



com.sun.eng
com.apple.quicktime.v2
Classes






Class names should be nouns
One word
The first letter is capitalized.
The first letter of each internal word is capitalized.
Keep your class names simple and descriptive.
Example:


Customer
SalesOrder
Naming Conventions - 2

Methods






Method names should be verbs
One word
The first letter is lower case.
The first letter of each internal word is capitalized.
Keep your method names simple and descriptive.
Example:




runFast
getBackground
showWelcomeMessage
Variables






Variable names should be nouns
One word
The first letter is lower case.
The first letter of each internal word is capitalized.
Keep your variable names simple and descriptive.
One-character variable names should be avoided except for temporary
"throwaway" variables
 Example:



firstName
phoneNumber
i (when it is a throwaway variable)
Naming Conventions - 3

Constants
 All
uppercase
 Words separated by underscores ("_").
 Example
static final int MIN_WIDTH = 4;
 static final int MAX_WIDTH = 999;
 static final int GET_THE_CPU = 1;

Where to Get More Information
Millions of people use code conventions
 This is available on the Java website:
java.sun.com
 There is a copy on the IT151 class
homepage.
