Control-Unusual Structures

xiaoxiao2021-03-06  91

Multiple Returns from a Routine1. Use a return when it enhances readability.2. Use guard clauses (early returns or exits) to simplify complex error processing.3. Minimize the number of returns in each routine.

Recursion1. Make sure the recursion stops.2. Use safety counters to prevent infinite recursion.3. Limit recursion to one routine.4. Keep an eye on the stack.5. Do not use recursion for factorials or fibonacci numbers.

Some Possible Rewrite Strategies for goto1. Rewrite with Nested if statements.2. Rewrite with a status variable.3. Rewrite with try-finally.

Guidelines for using gotos1. Use gotos to emulate structred control constructs in languages ​​that do not support them directly.2. Do not use the goto when an equivalent built-in construct is avaiable.3. Measure the performance of any goto used to improve efficiency.4. Limit yourself to one goto label per routine unless you're emulating structured constructs.5. Limit yourselft to gotos that go forward, not backward, unless you're emulating structured constructs.6. Make sure all goto labels are Used.7. make Sure a goto doesn't create unreachable code.

转载请注明原文地址:https://www.9cbs.com/read-107214.html

New Post(0)