1.6.11 Simplifying Complex Conditional Logic

Name the Decisions in a Soccer Rule

Long conditions are easier to review when meaningful Boolean locals name their parts. Simplification should preserve the requirement and the safe order of reference checks.

This example decides whether an existing player can take another training turn.

Start with the Soccer Example

Read the example once before trying to explain every line. Then use the short explanations below to connect each part of the code with the soccer task.

Name the Decisions in a Soccer Rule
public bool CanTakeTrainingTurn(Player selected, bool coachOverride)
{
    bool hasPlayer = selected != null;

    if (!hasPlayer)
    {
        return false;
    }

    bool hasTurnsRemaining = selected.NumberOfTurns < 3;
    return hasTurnsRemaining || coachOverride;
}
Understand One Part at a Time
Follow the Worked Trace

Read the table one row at a time. Check when each value is observed: a value before a statement may differ from the value after that statement runs. Cover the result column and predict an answer before checking it.

Worked soccer trace
Player Turns Override Returned result
Null Unavailable True False
Exists 2 False True
Exists 3 False False
Exists 3 True True
Follow the Same Idea Visually

Use the diagram to follow the same soccer example in a different form. Start at the beginning and follow one execution at a time. Compare the labeled actions with the source code; do not assume every action in the picture runs.

Name the Decisions in a Soccer Rule in the Wildcats soccer training model.
Name the Decisions in a Soccer Rule
  • New
  • Changed
  • Removed
Try a Small Change
Predict Before You Run

Write your prediction and a short reason first. If you test the change, compare the observed values with that prediction. When they differ, find the first statement or decision where your explanation stops matching the code.