Presentation is loading. Please wait.

Presentation is loading. Please wait.

Code Conventions Tonga Institute of Higher Education.

Similar presentations


Presentation on theme: "Code Conventions Tonga Institute of Higher Education."— Presentation transcript:

1 Code Conventions Tonga Institute of Higher Education

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

3 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.

4 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.

5 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

6 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.

7 Break after a comma Break after comma Subsequent lines use 1 tab

8 Break before an operator Subsequent lines use 1 tab Break before An operator Operator is first part of line

9 Indent all code within brackets This will help you debug your code Easy to see what is done In a method Easy to see what is done In a class Easy to see what is done in the body of the If statement

10 For IF conditionals, tab the subsequent line twice. Two tabs

11 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.

12 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

13 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

14 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)

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

16 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.


Download ppt "Code Conventions Tonga Institute of Higher Education."

Similar presentations


Ads by Google