loading...
Cover image for Code Complete 2: Statements (Part 4)

Code Complete 2: Statements (Part 4)

iyvonnebett profile image Iyvonne Bett Updated on ・2 min read

A statement is an instruction for the computer program to perform an action. There are many different types of statements that can be given in a computer program in order to direct the actions the program performs or to control the order that the actions are carried out in.

Chapter 14: Organizing Straight-Line Code

Statements That Must Be in a Specific Order
Organize code so that dependencies are obvious; if one method initializes data, create and call an Initialize() method.

Statements Whose Order Doesn't Matter
Statements that operate on the same data, perform similar tasks or have ordering dependencies should appear together.
Alt Text

Chapter 15: Using Conditionals
if statements
For both readability and performance, write the nominal path through the code first, then the unusual cases.
Simplify complicated conditional expressions with calls to methods that return boolean values.

Case statements
Some case statements only work on data of certain types; don't create a "phoney variable" to use a case statement.
Use the default statement to detect legitimate defaults, or to detect errors, and nothing else.

My Key takeaways in these two chapters are;

  • The strongest principle for organizing straight-line code is ordering dependencies.
  • Dependencies should be made obvious through the use of good routine names, parameter lists, comments, and—if the code is critical enough—housekeeping variables.
  • If code doesn’t have order dependencies, keep related statements as close together as possible.
  • For simple if-else statements, pay attention to the order of the if and else clauses, especially if they process a lot of errors. Make sure the nominal case is clear.
  • For if-then-else chains and case statements, choose an order that maximizes readability.
  • To trap errors, use the default clause in a case statement or the last else in a chain of if-then-else statements.
  • All control constructs are not created equal. Choose the control construct that’s most appropriate for each section of code.

Discussion

pic
Editor guide
 

Hi there. Nice article.

If you make this series(Dev.to has that feature). It may improve reader experience