Talks by Sandi Metz

All the Little Things

General Wisdom

Changes in shape (indentation) mean you have nested conditionals (mixed scopes).

Changing colors means your code is at differing levels of abstraction.

Rush to green and then refactor.

Duplication is far cheaper than the wrong abstraction.

Abstractions add complexity, and increased complexity with the wrong abstraction is infinitely worse than simply duplicating a low-complexity solution.

Reach for open / closed.

The Open / Closed principle makes changes easy. If code never changes, then it won't break.

Refactor through complexity... Refactor to simplicity.

When refactoring, you should be trying to reduce the number of changes in the shape of your code (the depth, or indentation of your code) and the intermixing of colors in your code (interspersed abstractions). While refactoring, complexity may increase - but the goal is to ultimately reduce the complexity. This is done by taking large, nested, mixed blocks of code and breaking them into small methods on small objects which will be faster and easier to reason about / test.

SOLID Object-Oriented Design

Vocabulary

  • Every time rigid code changes, it causes a cascade of related changes.
  • Fragile code is unpredictably rigid.
  • Immobile code must be copied rather than imported to be reused.
  • Viscous code encourages you to adopt unintended patterns or otherwise misuse it.
There should never be more than one reason for a class to change.
A module should be open for extension, but closed for modification.
Subclasses should be substitutable for their base classes.
Many client specific interfaces are better than one general purpose interface.
Depend upon abstraction. Do not depend upon concretions.

Code design, like testing, is initially expensive, but rewarding in the long term. Design reduces the cost of change over time.

  • Is the class DRY?
  • Does the class have one responsibility?
  • Does everything in the class change at the same rate?
  • Does the class depend on things that change less often than it does?