a feature of an algorithm that a special case is handled fortuitously, by the general
case, without requiring special code.
When you are coding, to start, forget all the exceptional cases. Focus on the code
for the ordinary cases first, streamlining and simplifying it, starting from the
innermost loops and working out.
Then gradually add code to deal with the oddball things. If your code is clean,
you will discover that there is nothing to do. The code you have written already
automatically handles the weird corner cases properly. If you find yourself adding on
bits of fluff to handle every special case, relook at how you coded the ordinary
cases and see if there is a way of modifying in so the odd cases come out in the
wash without special coding.
Code where everything neatly comes out in the wash is:
- easier to read and understand.
- more likely to be bug free. Odds are it will also correctly handle other
pathological conditions too that you did not think of.
- faster.
When you later add in the code your special cases, endeavor to put the
special-case code in the else clauses, not the then
clause (right after the if). This has two advantages:
- It makes it clearer to the maintenance programmer which is the ordinary case
and with the special.
- The code will run slightly faster with the default fall through from the if
test.
Note that this elegance (the mathematical word for
beautifully simple) is often a property of a data structure, not of the algorithm per
se. For instance, using a head node with a linked list obviates special-case code
that handles the oddball empty-list cases.
Etymology
I can only speculate on the etymology. It may be
that a general washing cleans away individual stains. It may refer to robustness, a
dye that is colourfast to repeated washings. One Brit told me the phrase means that
we’ll find out whether something is really a good idea when we apply it in the
real world, the way you find out how well made your new clothes are the first time
you wash them. Another assured me the phrase refers to when a problem or thing naturally emerges as the result of a process e.g. the concealed
truth that underlies a scandal may be said to eventually come
out in the wash, or a coin left in your trousers may come out in the wash.