Code Conventions Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Commenting and Naming Conventions
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
1 CS1001 Lecture Overview Java Programming Java Programming.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
26-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
Agile Lab 2008a Armin B. Cremers, Günter Kniesel, Pascal Bihler, Tobias Rho, Marc Schmatz, Daniel Speicher Sommersemester 2008 R O O T S Coding standards.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Programming Style and Documentation Objective(s) F To become familiar with Java Style and Documentation Guidelines.
Java Language and SW Dev’t
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
The Java Programming Language
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Java Coding Standards and Best Practices Coding Standards Introduction: After completing this chapter, you will able to keep your code up to standards.
AP Computer Science Programming Conventions. Why coding conventions? 80% of the lifetime cost of a piece of software goes to maintenance. Hardly any.
1 written by BAGE 날짜 JAVA Coding Pattern 코딩 규칙 박혜웅.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
JAVA Programming “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
Writing Maintainable code Dr. Susan McKeever DT228/3 GUI Programming.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Working With Objects Tonga Institute of Higher Education.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Working with Java.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Coding Design, Style, Documentation and Optimization
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Chapter 4 void Functions
Variables in C Topics Naming Variables Declaring Variables
Tonga Institute of Higher Education
Tonga Institute of Higher Education
Unit-1 Introduction to Java
Chapter 1: Computer Systems
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Tonga Institute of Higher Education
Documentation and Style
Beginning Style 27-Feb-19.
Tonga Institute of Higher Education
Topics Introduction to Functions Defining and Calling a Function
Coding practices For IT and Computing students 2014.
Chap 2. Identifiers, Keywords, and Types
Tonga Institute of Higher Education
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CSCE-221 C++ Coding Standard/Guidelines
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

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 % 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 ed to me for help that does not follow code conventions.

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

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 Subsequent lines use 1 tab Break before An operator 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 method Easy to see what is done In a class 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. 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

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.