Troppo pigro per scrivere qualcosa di mio…

Summary: nice exchange on the TDD list

Phlip:

Change your code to meet these ideals, in this order:

  • 0. matches your community agreements on style
  • 1. passes all tests
  • 2. clear and expressive code
  • 3. no duplication of _behavior_
  • 4. minimal classes, lines, and methods

Item 0 goes before 1 because you can’t TDD good style. Things like
when to use a reference or pointer, when to capitalize, etc. This is
too low-level to TDD, so just write it.

Item 1 is TDD. Do this before Refactoring, which is 2., 3., and 4.

Item 2 is before item 3 because for some languages (such as C++) if
you squeeze out the last drop of duplication, you will have unreadable
code. Only remove duplication in ways that improve the style.

And 4 is your “simplify” thing. Balance out the classes and levelize
your packages.

Jeffries:

I personally refactor for all the purposes you list, and perhaps more. Removing duplication, for me, is the first thing I look for, and still carries much of the weight, especially as I learn new ways of recognizing duplication. It’s a good first step to better code.

Phlip:

To teach us to improve things, we spread the definition of “duplication” as wide as possible, beyond mere code cloning. For example, the numbers 4 and 6 may, in context, actually represent 5-1 and 5+1. Remove optimizations to inspect latent underlying concepts.

When we refactor 4 and 6 into their actual behaviors, 5-1 and 5+1, we are increasing the amount of duplication on purpose, to then merge it. The only difference between those expressions is the – and +.

In general, if you have two situations that look generally the same, you should should refactor them, separately, until they are more similar. Then you might merge them together. Put another way, don’t merge them together until you first prove they are equivalent.

Jeff L:

As fasr as why “only” emphasize eliminating duplication: first, the
presumption is that we’re doing TDD, otherwise we don’t have the
confidence to do much of anything with the code. Doing just TDD should
already give us a significantly better design, because of its impacts
on coupling and correctness.

If we just did TDD and eliminated all duplication, our system would be
of significantly higher quality than 99.9% of all other systems out
there. Maybe that’s all we need.

Leave a Reply