Coding Conventions Coding conventions are a set of guidelines for a specific software project that...

13
Coding Conventions Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Transcript of Coding Conventions Coding conventions are a set of guidelines for a specific software project that...

Page 1: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Coding Conventions Coding conventions are a set of

guidelines for a specific software project that recommend programming style, practices and methods.

Page 2: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

File organization Common well-structured folder hierarchy Folder templates Restrict user-creation folders to specific

subfolders Do not create overlapping categories Many times file organization is dictated by

the IDE or repository

Page 3: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Indentation Examples of nesting rules:

Number of spaces indented after a function header

Number of spaces indented in a nesting Increased readability is the goal

Page 4: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Comments Types of comments

Descriptive blocks Inline comments Class comments (Descriptive block) Method comments (Descriptive block) Variable comments (Inline)

Comment while coding Avoid obvious comments Leave descriptive comments

Page 5: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Declarations One or multiple declarations per line Scope rules - modifiers

Page 6: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Statements Forbidden statements (goto!) Multiple criteria rules (use switch instead

of nested ifs for >2 criteria) Maximum levels of nesting Preferred use (should use if then else

instead of ternary operator)  Line length limits

Page 7: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

White space Line spacing Spaces between keywords

Page 8: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Naming Conventions Identifiers must be descriptive Use underscore to separate words Each word starts with a capital letter camelCase: First letter of each word is

capitalized, except the first word Some developers prefer to use

underscores for procedural functions, and class names, but use camelCase for class method names

Page 9: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Naming Conventions Consistent temporary names Capitalize SQL special words

SELECT id, username FROM user;

Page 10: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Grouping Conventions More often than not, certain tasks require

a few lines of code. It is a good idea to keep these tasks

within separate blocks of code, with some spaces between them.

Page 11: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Grouping Conventions // get list of forums $forums = array(); $r = mysql_query("SELECT id, name, description FROM

forums"); while ($d = mysql_fetch_assoc($r)) { $forums []= $d; } // load the templates load_template('header'); load_template('forum_list',$forums); load_template('footer');

Page 12: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Separate Code and Data Rules should be enforced to separate

code from data This is the root cause of every buffer

overrun/overflow exploit

Page 13: Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.

Duplicate Code DRY stands for Don't Repeat Yourself. Also known as DIE: Duplication is Evil. The principle states:"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."