liveBook · Manning

Complexity metric

As explained in High Performance Unity Game Development

A measure of the structural intricacy and difficulty of understanding, modifying, or executing software code.

A survival game’s update loop may spawn enemies, move them, handle collisions, and check for game over. Counting branches in that loop is a practical complexity metric: each `if` or other conditional adds a path whose condition and outcome must be understood. A branch-free sequence makes the work that runs on every tick easier to see. section 11.0

Listing 11.1 The Tick() function without any branches
public static void Tick()
{
    spawnEnemy();
 
    moveEnemies();
 
    checkEnemyOutOfBounds();
 
    doEnemyToEnemyCollision();
 
    movePlayer();
 
    checkGameOver();
}

This `Tick()` calls its gameplay steps in sequence, without deciding whether to call each one. section 11.0

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();
}

Compare the conditional version: debugging now requires checking which steps ran and why. section 11.0

Branch count is a useful signal, not a complete score for a codebase. Separating alive and dead enemy indices removes a status check from each enemy iteration, but introduces arrays and index bookkeeping. The trade-off can be worthwhile when it simplifies a frequently run loop. Branches in hot code may also cost CPU work, especially when their outcomes are unpredictable; measure performance rather than assuming every conditional needs removal. section 11.0 section 11.7

Look beyond visible conditionals. Repeated Unity `GameObject` null checks can perform extra native-lifetime work; checking assets when they load and controlling object lifetimes can avoid unnecessary checks during play. Conversely, reducing branch count is not a reason to add architecture indiscriminately: a full ECS conversion can create more components, systems, and conversion work than an array-based game needs. section 11.7 section 12.3

For UI that behaves differently across menus, separate scripts can be easier to debug than one shared script full of cases. Keep shared appearance in assets and data. The aim is not zero branches everywhere; it is code whose paths, data, and maintenance costs are easy to see. section 11.5 section 1.1

How High Performance Unity Game Development covers it

How deep it goes
Explains the metric through contrasting update loops and practical trade-offs, without defining a formal composite score.
Examples
Contrasts two Tick() implementations and uses alive-versus-dead enemy arrays and menu top bars as design cases.
What it assumes
Assumes familiarity with basic conditionals and gameplay update functions; introduces branch counting through a concrete comparison.
What you can do after
Evaluate/understand it by counting branches, tracing conditional paths, and weighing simpler loops against added bookkeeping.
How it connects
Connects branching to branch prediction, data-oriented design, object lifetimes, and Entity Component System overhead.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.