Programming Style Guide

Naming Conventions

No abbreviations or acronyms for variable names

Underscores for variable names and backend file names

Dashes for frontend file names

Hungarian notation for function names and query names

Prefix each variable name initialization with a scope(private, local, public, global)

Prefix each function name with it’s class name

Quoting Conventions

Doublequotes inside parentheses and in string declarations

Singlequotes inside doublequotes

Notation Conventions

Use dot notation for referencing standard attribute names

Use square bracket notation for referencing evaluated variable names in loops or conditional statements

Comment Conventions

Use line breaks and comments in regular expressions to make them legible

Preface scripts and large functions with a descriptive block which explains the purpose and usage of the code as well as the history, author, dependencies, parameters, and callers of the functions

Use inline comments for describing algorithms for loops and complex data structures

Create public versions of CSS, HTML, and Javascript code with comments and whitespace removed

Use keywords such as TODO:, FIXME:, NOTE:, REFERENCE:, etc to denote clauses for work in progress or experimental code

Block level Indentation/Spacing Conventions

Functions, loops, and conditional statements should have opening and closing brackets that are equally indented

One line conditional statements should still include brackets

Replace conditional statements with conditional function calls or boolean function calls

// imperative if/else statement
if (cond1)
  {func1()}
else if (cond2)
  {func2()}
else
  {func3()}
//functional branch
((cond1) and func1()) or ((cond2) and func2()) or (func3())

for loops can be directly translated to map() or each() functions

for e in lst:  func(e)      # statement-based loop
map(func,lst)           # map()-based loop

while loops can be translated to a function with a monad

# imperative version of "echo()"
def echo_IMP():
     while 1: x = raw_input("IMP -- ")
          if x == 'quit': break
          else print x echo_IMP()
# utility function for "identity with side-effect"
def monadic_print(x):
     print x
     return x
# FP version of "echo()"
echo_FP = lambda: monadic_print(raw_input("FP -- "))=='quit' or echo_FP() echo_FP()

nested loops can be replaced by much simpler and readable list comprehension

# Nested loop procedural style for finding numeric pair with a product value greater than 25
xs = (1,2,3,4)
ys = (10,15,3,22)
bigmuls = []
# ...more stuff...
for x in xs:
     for y in ys:
     # ...more stuff...
          if x*y > 25:
              bigmuls.append((x,y))
              # ...more stuff... # ...more stuff...
print bigmuls

# List comprehension version of the same logic
print [(x,y) for x in (1,2,3,4) for y in (10,15,3,22) if x*y > 25]

Put a single space after loops, a double space after functions

Put each database field name on a separate line with a leading comma for more than three columns

Don’t allow horizontal scrolling for any lines of code

Put inline comments at the beginning of any conditional statement or loop that’s ten lines or more

Data Structure Conventions

Use hashes to store lists of related key/value pairs instead of assigning each to a variable

Function Conventions

Use a delegation pattern to handle events whenever possible

Use a closure pattern to pass static variables to dynamic functions that return different values

References

JavaScript Conventions
How to Write Unmaintainable Code
How to Write Clear Code
Objective-C Coding Guidelines

TrackBack URI

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.