Sub topic 4
Write Maintainable Spreadsheet Formulas
Assume you know basic spreadsheet navigation, arithmetic, percentages, and the game system you are balancing. This chapter focuses on formula design: making calculations readable, reusable, and safe to change.
A maintainable formula makes its assumptions visible. It avoids unexplained constants, uses consistent references, and separates raw inputs from calculated values. This matters because balance work involves repeated changes. If changing one weapon requires editing ten unrelated formulas, the spreadsheet will become unreliable.
Use clear references
Keep values in separate areas:
- Inputs: values chosen by the designer, such as attack power or enemy health.
- Calculations: formulas that derive combat or economy metrics.
- Outputs: values used for decisions, such as time to kill or total upgrade cost.
Use absolute references for shared inputs. An absolute reference does not move when a formula is copied. In most spreadsheets, $B$2 refers permanently to cell B2. A relative reference such as B2 changes when copied.
For example, suppose B2 contains a weapon's attack power and B3 contains an enemy's defense. A damage formula can be written as:
=MAX(1, $B$2 - $B$3)MAX(1, ...) enforces a design rule: a successful attack deals at least one damage. The dollar signs keep the input cells fixed when you copy the formula to another row.
Avoid magic numbers
A magic number is a value embedded in a formula without a clear explanation. For example:
=MAX(1, AttackPower - 25)The value 25 might be enemy defense, but the formula does not say so. Put that value in an input cell and reference it by position or, preferably, by a named range. A named range such as EnemyDefense makes the rule easier to read:
=MAX(1, AttackPower - EnemyDefense)Use one unit for each column. Label whether a value means seconds, attacks per second, health points, or resources. Do not mix “seconds per attack” with “attacks per second” without converting one of them.
| Metric | Formula pattern | Unit |
|---|---|---|
| Damage per hit | `MAX(1, AttackPower - Defense)` | health points |
| Time to defeat | `CEILING(Health / Damage / HitsPerSecond, 1)` | seconds |
| Relative change | `IFERROR((New - Baseline) / Baseline, 0)` | percentage |
Build formulas from smaller results
Long formulas are difficult to inspect. Calculate intermediate values in separate columns:
- Calculate damage per hit.
- Calculate hits required.
- Calculate time to defeat.
- Compare that time with the target.
For a target with health, damage per hit, and attack frequency, use:
=CEILING(TargetHealth / DamagePerHit / HitsPerSecond, 1)CEILING rounds up because a partial attack cannot defeat a target. If TargetHealth is 100, DamagePerHit is 20, and HitsPerSecond is 2, the result is 3 seconds.
Handle copied formulas and errors
Progression tables often need a cumulative total. If costs begin in B2, enter this formula in C2 and copy it downward:
=SUM($B$2:B2)The first reference stays fixed. The second expands as the formula moves down. This produces the running total for each upgrade level.
When comparing a new value with a baseline, protect the formula from division by zero:
=IFERROR((NewValue - BaselineValue) / BaselineValue, 0)A zero result means the comparison could not be calculated. Record that limitation in a note rather than treating it as proof that there was no change.
Exercise
A weapon has 120 attack power, an enemy has 30 defense, and the weapon attacks twice per second. Write formulas for damage per hit and whole seconds to defeat an enemy with 180 health.
Show answer
Damage per hit: =MAX(1, 120 - 30), which gives 90. Time to defeat: =CEILING(180 / 90 / 2, 1), which gives 1 second.