Magic Numbers Represent Concepts

Literals represent something.[p 94, 1] Magic numbers are a form of literal.

The number 79 can truly represent the result of 80–1.

When height = 80 and offset = 1, the resulting number 79 represents a relation between the height and an offset.

Working with “magic numbers”, that is numbers that are not named using variables/constants in the first place, poses a problem when constraints or relations change. You cannot reliably “Search & Replace” every occurence of 79 in your project with height - offset – some 79’s could have meant something different before.

Thinking about readability first when you write code reduces these problems to a minimum later. Give literals meaningful names.

Swift accidentally provides namespaces for stuff like this with enums without cases. Then you don’t clutter your functions with constant definitions. The following example is quite convoluted, but brings the point across:

enum Layout {
    static let height = 80
    static let width = 100
    static let padding = 1
    static let margin = (top: 0, right: 10, bottom: 0, left: 10)

func putStuffOnScreen() {
    let previousFrame: CGRect = // ...
    // ... 
    let newFrame = CGRect(
        x: previousFrame.x + previousFrame.width + Layout.margin.left,
        y: previousFrame.y +, 
        width: Layout.width + 2 * Layout.padding,
        height: Layout.height + 2 * Layout.padding)
    // ...

  1. Edward Yourdon and Larry L. Constantine (1979): Structured design, Englewood Cliffs, N.J.: Prentice-Hall.

Browse the blog archive