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
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
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
Declarations One or multiple declarations per line Scope rules - modifiers
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
White space Line spacing Spaces between keywords
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
Naming Conventions Consistent temporary names Capitalize SQL special words SELECT id, username FROM user;
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.
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');
Separate Code and Data Rules should be enforced to separate code from data This is the root cause of every buffer overrun/overflow exploit
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."