Skip to Content
Tidy First?
book

Tidy First?

by Kent Beck
October 2023
Intermediate to advanced
122 pages
1h 58m
English
O'Reilly Media, Inc.
Book available
Content preview from Tidy First?

Chapter 3. Normalize Symmetries

Code grows organically. Some folks use “organic” as a pejorative. That makes no sense to me. We can’t possibly write all the code we’ll ever need all at once. That would only work if we never learned anything.

In growing organically, the same problem may be solved differently at different times or by different people. That’s okay, but it makes for difficult reading. As a reader, you’d like consistency. If you see a pattern, you can confidently jump to the conclusion that you know what’s going on.

Take the example of lazily initialized variables. You might see them written different ways:

foo()
    return foo if foo not nil
    foo := ...
    return foo

foo()
    if foo is nil
        foo := ...
    return foo

# tricky
foo()
    return foo not nil
        ? foo
        : foo := ...

# doubly tricky, assuming assignment is an expression
foo()
    return foo := foo not nil
        ? foo
        : ...

# even trickier, hiding the conditional
foo()
    return foo := foo || ...

(See if you can find or invent more variants.)

All of these are ways of saying, “Compute and cache a value for foo if we haven’t already.” Each has its pros and cons. You as a reader will quickly get acclimated to any one of them. Things get confusing when two or more of the patterns are used interchangeably. As a reader, you expect that difference means difference. Here you have difference that obscures the fact that the same thing is going on.

Pick a way. Convert one of the variants into that way. Tidy one form of unnecessary variation at a time—lazy ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Building Micro-Frontends

Building Micro-Frontends

Luca Mezzalira
Learning Go

Learning Go

Jon Bodner
Head First Git

Head First Git

Raju Gandhi

Publisher Resources

ISBN: 9781098151232Errata Page