Code Conventions Statement blocks

From MapbenderWiki

Jump to: navigation, search

Layout

  • use indentation to enhance logical structure (2 to 4 spaces = 1 tab)
  • always use curly braces, even if the block consists of only one line
  • put the opening curly brace in the same line with previous keyword and the closing one in a single line
Example:

bad

if (isPrinterReady) 
{
 ...
} else { 

good

if (isPrinterReady) {
 ...
}
else {
 ...
}
  • use spaces to enhance readability (before and after operators like &&, ==, >, ||)

Conditionals (If...Else)

  • write the nominal path first, then write unusual cases (normal case in if branch, unusual case in else branch)
  • make sure all cases are covered (f.e. always add final else to a construct with a lot of else ifs)
  • compare to true and false implicitly (write if (! found) instead of if (found == false))
  • fully parenthesize logical expressions (maybe even replace expression by routine)
  • avoid mixed-type comparisons
  • avoid equality comparisons with numerals like floats

Loops

  • initialize variables used in a loop immediately before the loop (not at the beginning of a function)
  • check if the loop does terminate under all conditions
  • avoid nestings deeper than 3 (write opening and closing brackets at the beginning to avoid problems)
Views
Personal tools