syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 8 : Language Usage

PART 4 : USAGE

CHAPTER 8 : Language Usage
8.1 General principles of language usage
8.2 Using expressions
8.3 Using 'if'
8.4 Using 'while'
8.5 Using 'for'
8.6 Using 'do'
8.7 Using 'switch'
8.8 Using 'goto'
8.9 Using 'continue' and 'break'
8.10 Using 'return'
8.11 Using functions
8.12 Using '#define'
8.13 Conditional compilation
8.14 Other preprocessor commands
8.15 Summary

<--Prev page | Next page -->

 

8.15  Summary

  • Break down complex expressions, declarations, etc. into several simpler items.
  • Use appropriate constructs for appropriate circumstances.
  • Avoid obscure, implicit and hazardous features.
  • Avoid deep nesting of statements, parentheses and structures.
  • Use if..else rather than conditional expressions ( () ? : ).
  • Only use bit-manipulations expressions when it is essential.
  • Beware of assignments in expressions increasing complexity.
  • Avoid assignment in comparisons, except where the alternative is significantly more complex.
  • All non-boolean comparison expressions should use comparison operator (don't use implicit '!= 0').
  • Don't use comparisons in expressions.
  • Minimize use of the comma operator.
  • Use explicit casting, rather than using the compiler default.
  • Cast the null pointer (NULL) to the pointer type.
  • If you are not using a function return, cast it to void.
  • Be careful with autoincrement and autodecrement. Use them consistently.
     
  • Use nested if only to force the order of evaluation.
  • Minimize negative comparisons.
  • Use if..else for two alternative actions.
  • Use else..if as a complex 'switch' construct. Order the comparisons either for performance or readability.
     
  • Minimize code in loops after the comparison has become false.
  • Count for loops from 0 to < max_value.
  • Use for loops when the loop control needs initializing or recalculating, otherwise use while.
  • Beware of the for statement becoming over-complex with non-loop-control initializations, etc.
  • Decide on one 'infinite loop' construct, and stick to it. Make its usage clear with #define / comments.
     
  • Be careful with the logic of do loops. Use do..while(!(..)) to loop until a comparison becomes true.
     
  • In a switch statement, make all cases independent by using break at the end of each (or clearly comment any fallthrough). Also cater for unexpected cases with default.
  • When case's get long (eg. over 7 lines), break then out into a function.
     
  • Use goto sparingly, eg. for escaping deep levels of nesting. When using it, name labels to indicate if it marks the start or end of a piece of code.
  • Minimize use of break in loops. Only use it for abnormal escape.
  • Avoid using continue (use if..else instead).
     
  • Only use return in mid-function for abnormal return.
  • Return a status value from 'main()' with 'exit()'.
     
  • Always declare the function return type, including int and void.
  • Avoid large numbers of function parameters. Use structures where appropriate.
     
  • Don't re-#define the language.
  • Be very careful with macros:
    - Use parentheses and comma operator to ensure a single expression.
    - Use do..while(0) to ensure a single statement.
    - Only use a macro if there is a significant benefit.
     
  • Minimize conditional compilation in the main code.

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |

 

You can buy books here

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-2015
Massive Content -- Maximum Speed