Continuous Design

Explore my other writings
2023-12-13

Reading notes: Tidy First? A Personal Exercise in Empirical Software Design

This is a short summary, or rather reading notes, about the book Tidy First? A Personal Exercise in Empirical Software Design by Kent Beck.

tidyfirst.jpg

Kent Beck’s new book talks about software design, introducing a definition to explore in a future book series:

Software design is an exercise in human relationships

Nothing technical about design, because design is not achieved for the computer. Design is the code structure and it doesn’t directly impact behavior.

The structure of the system doesn’t matter to its behavior.

So why should we bother about design ?

The answer, as always, is because you are not just instructing a computer, you are explaining your intentions for the computer to other people. The shortest path to instructing the computer is not an interesting end goal.

This book describes different means to tidy the code, using a short catalog of pragmatic code modifications. Then Kent Beck explains how it is related to the development process and when to apply these tidyings. In the last section he explains why it works, why software design creates as much value than behavior or features—but value of a different nature.

What is tidying?

Essentially, tidyings are refactorings, but the smallest ones, so small that nobody can argue against them.

The usual situation is:

So, first, make the design like we need.

We change the structure to obtain the structure that will facilitate the next change in behavior.

But there are also two other use cases:

Tidying Catalog

Kent beck provides a catalog of 15 tidyings:

How to use these Tidyings in the development cycle

When tidying code, we separate tidyings and bevahior changes in different Pull Requests (PRs).
Kent Beck gives an example of a big PR with a sequence of behavior (B) and structure (S) changes:
[BSSBSSSBBS]
He proposes to split it in different PRs:
[B] [SS] [B] [SSS] [BB] [S]

Hence, sometimes you need to:

About the tidying PRs there are pitfalls to worry about:

At this point, it is logical to wonder if we will enter in an infinite loop: tidy the structure, change the behavior, change the structure, change the behavior, and so on. Kent beck reminds us the Pareto principle: 80% of the changes occurs in 20% of the code. In other words, after the first tidyings are done, it will occur more and more frequently to work in an already tidied code.

The right moment to apply tidyings

Before to start tidying, we need to understand there is a problem in design, that our current task should be easier if we had a different code structure.

Untangling a ball of yarn starts with noticing that you have a tangle.

And sometimes we discover it only after we are done. Or maybe it’s too much effort for a code that barely change.

Of course, Kent Beck always encourages us to tidy the code, and before any other change: a tidy job done sooner is smaller.

But he also provides the following heuristic:

Tidy never when:

  • You’re never changing this code again.
  • There’s nothing to learn by improving the design.

Tidy later when:

  • You have a big batch of tidying to do without immediate payoff.
  • There’s eventual payoff for completing the tidying.
  • You can tidy in little batches.

Tidy after when:

  • Waiting until next time to tidy first will be more expensive.
  • You won’t feel a sense of completion if you don’t tidy after.

Tidy first when:

  • It will pay off immediately, either in improved comprehension or in cheaper behavior changes.
  • You know what to tidy and how.

Design as a software value

The value of a software is usually estimated by its features and its capabilities, in other word its behavior. Tidying and refactoring are only working about design and structure of the code. It brings no value.

Instead, structure can create options.

Design makes it easier to change behavior—or makes it harder.

We need to tidy only if it creates options (changing the structure, learning…)

Kent Beck explains it with this formula:

cost(tidying) + cost(behavior change after tidying) < cost(behavior change without tidying)

In this case, there is no question. Apply tidyings it brings more value (or it brings value earlier, if reduced cost is about delivery time).

But what if we are in this situation:

cost(tidying) + cost(behavior change after tidying) > cost(behavior change without tidying)

We need to estimate the value of options created by our tidying. The amortized cost of these options may be worth it. It’s hard to say, here we let our experience and skills take the decision.

Tidyings listed in the catalog above are not big changes or long refactoring, they should limit the discussion about their usefulness.

Coupling and Cohesion

The last chapters of Tidy First? are dedicated to the concepts of Coupling and Cohesion.

Coupling is a relation between two elements: changing one element requires to change the other. Nature of the change is as important as the relation. For instance, a change that never happens is a coupling that doesn’t bother us.

So coupling is not a problem until we need do a specific change

But under some circumstances, coupling is expensive. The cost of a software, is the cost of its change: the initial write to first release is nothing compared to the cumulative cost of all evolutions during its lifetime. And big changes are very expensive compared to the cost of all minor changes. A big change is where we have to impact several parts in the software: our modifications are spread in codebase or we don’t know a change will have some impacts on other behaviors… or even break them (directly in production, most of the time).

In summary:

cost(software) ~= coupling

But remember coupling is not a problem: it’s here for reason, either it was logical at some time, or it wasn’t a problem until we need to change a specific behavior.

Question is: do we pay the cost of coupling (extra care, complexity) or the cost of decoupling (tidying first)?

Decoupling is creating more options for now or for future, so it’s valuable.

Cohesion is what we obtain when we arrange coupled elements together. Kent Beck summarize two way to obtain cohesion:

Tidyings are about creating decoupling and creating cohesion.

Conclusion

Kent Beck calls “tidyings” the small changes we can make about expressiveness, coupling and cohesion. So small they have quite no cost in time and effort. But with high impact on code structure and optionality.

At least they help us to be more happy about our work, which is a real indicator for Kent Beck:

Will tidying bring peace, satisfaction, and joy to your programming? Maybe some. This is important because if you are your best self, you are a better programmer. You can’t be your best self if you’re always rushing, if you’re always changing code that’s painful to change.

Big refactors will come. In this book we are mainly talking about the daily diffs we can make to our code to make the whole work easier.

Make no sudden moves. You’re working with incomplete and changing information about what’s coupled with what. Don’t dramatically rearrange everything. Move one element at a time. Make the code tidier for the next person. If everyone follows the Scout rule (“leave it better than you found it”), the code will become more livable-with over time.

We need to make tidyings a concrete thing, in adapted PRs, and respect these improvements even the smallest: this is real work, creating options—creating value.

To go further:

Youtube: A Daily Practice of Empirical Software Design - Kent Beck - DDD Europe 2023


Home