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.
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.
Chapter 15: Using Conditionals
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.
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.