A game loop may need to spawn enemies, move them, handle collisions, and check for game over. A branch—such as an if, switch, or conditional operator—selects which work runs. Each added condition also gives a reader or debugger another path to consider. section 11.0 section 5.2.5
Listing 11.2 The Tick() function with a lot of branches
public static void Tick()
{
if(canSpawnEnemy())
spawnEnemy();
if(canMoveEnemies())
moveEnemies();
if(shouldBoundCheck())
checkEnemyOutOfBounds();
if(collisionsOn())
doEnemyToEnemyCollision();
if(canMovePlayer())
movePlayer();
checkGameOver();
}The conditional Tick() makes each game-loop step depend on a separate decision. Compare it with the straight-line Tick() in the same section: the latter makes the sequence of calls immediately visible. section 11.0
Branches in hot loops add instructions and can disrupt the CPU's work when a predicted path is wrong. Unpredictable conditions are especially costly; even a predictable check can cost more than a branchless alternative. This is a reason to examine repeated checks, not to remove every if statement. section 11.0 section 11.8
Move decisions to where they are needed, rather than repeating them for every object or hiding them inside shared logic:
- Keep alive and dead enemy indices in separate arrays, then iterate the alive array without testing every enemy's status. Moving indices between arrays requires bookkeeping. section 11.0
- Keep an input decision in the board's input handler instead of passing all input to a deeper logic function that must make the same decision. section 5.2.5
- Use separate scripts for menu top bars whose behavior differs, rather than growing one shared script full of menu-specific conditions. Share simple, single-purpose logic where appropriate. section 11.5 section 7.1.8
Watch for less visible decisions too. A defaulted C# parameter can conceal a conditional inside a function whose call shows no such argument. Repeated Unity GameObject null checks also do more work than an ordinary reference check; when ownership and loading rules permit, check once rather than repeatedly in a hot loop. Prefer the simpler structure when the extra arrays or scripts would not earn their maintenance cost. section 11.6 section 11.7 section 11.0






