Saturday, May 14, 2011

Code Complete by Steve McConnell : Chapter 11

This chapter provides some tips to use primitive data types.
  • Avoid using number literals directly. Use named constants instead. This makes code easily modifiable and readable.
  • Anticipate divide by zero errors.
  • Use type conversions explicitly.
  • Avoid mixed type comparisons.
  • Never ignore compiler warnings.
  • Take proper care for integer divisions.
  • Check for integer overflow.
  • Check for overflow in intermediate results.
  • Avoid additions and subtractions of floating point numbers with a very different magnitude.
  • Never do equality comparison between floating point numbers.
  • Anticipate rounding errors in floating point numbers.
  • Avoid magic characters and strings
  • Avoid off-by-one errors
  • Initialize strings to null and use strncpy instead of strcpy to avoid endless strings
  • Use arrays of characters instead of pointers
  • Use boolean variables to document conditions
  • Enumerated types instead of integer or string literals increase readability
  • Enumerated types in some languages are type-checked which increases reliability
  • Enumerated types increase modifiability/maintainability
  • Have the first enumerated value as invalid
  • Check that array indexes are within the bounds
  • Use correct subscripts for multi-dimensional arrays
  • Isolate pointer operations in routines
  • Check pointers and variables referenced by the pointers before using it
  • Set pointers to NULL after freeing them

No comments:

Post a Comment