code conventions tonga institute of higher education

16
Code Conventions Tonga Institute of Higher Education

Upload: william-richardson

Post on 14-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Code Conventions Tonga Institute of Higher Education

Code Conventions

Tonga Institute of Higher Education

Page 2: 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.

Page 3: Code Conventions Tonga Institute of Higher Education

Why Have Code ConventionsCode 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.

Page 4: Code Conventions Tonga Institute of Higher Education

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.

Page 5: Code Conventions Tonga Institute of Higher Education

File Organization

Source Code should be written in the following order:

1. Comment flowerbox.

2. Package Statements package java.awt;

3. Import statements import java.awt.peer.CanvasPeer;

4. Class or Interface Statement

5. Variables

6. Constructors

7. Methods

Page 6: Code Conventions Tonga Institute of Higher Education

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.

Page 7: Code Conventions Tonga Institute of Higher Education

Break after a comma

Break aftercomma

Subsequent lines use 1 tab

Page 8: Code Conventions Tonga Institute of Higher Education

Break before an operator

Subsequent lines use 1 tab

Break before An operator

Operator is firstpart of line

Page 9: Code Conventions Tonga Institute of Higher Education

Indent all code within brackets This will help you debug your code

Easy to seewhat is doneIn a method

Easy to seewhat is doneIn a class

Easy to seewhat is done inthe body of the Ifstatement

Page 10: Code Conventions Tonga Institute of Higher Education

For IF conditionals, tab the subsequent line twice.

Twotabs

Page 11: Code Conventions Tonga Institute of Higher Education

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.

Page 12: Code Conventions Tonga Institute of Higher Education

Blanks Spaces and Lines

Blank Spaces A blank space should separate keywords such as if, for, etc.

Example: if (x == y) { All binary operators (+, -, <, ==, etc.) should be separated by spaces.

Example: x = 5 + 3; A blank space should appear after commas in argument lists.

someClass.someMethod(x, y, z); A blank space should separate brackets

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

Page 13: Code Conventions Tonga Institute of Higher Education

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

Page 14: Code Conventions Tonga Institute of Higher Education

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)

Page 15: Code Conventions Tonga Institute of Higher Education

Naming Conventions - 3

ConstantsAll 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;

Page 16: Code Conventions Tonga Institute of Higher Education

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.