Legacy code smells: double meanings
I am currently working on a legacy system. It’s not very scalable, not very maintainable, and is regarded pretty much as a dragon that needs slaying.
But how did it get that way?
Its not just because its old — emacs, vi and the gnu utilities are 20+ years old but they are not ‘legacy’.
There’s a whole lot of smells that characterise legacy systems, and I’m going to try and tease them out over the next few weeks…
One smell that really hurt us is double meanings. It happens when change to some part of the system is too hard (or too scary), so rather than introducing a new field or column, you reuse an old one, wither changing its meaning or introducing an extra meaning.
You tend to see this a lot in database designs. Since its so hard to change schemas, and migrate data to a new schema, you look for a column or field that is not used or rarely used, and give it a new meaning, with a new flag.
To give a concrete example, a table in our system has a column called error_ind which takes the values “Y”, “N” and “E”. “Y” and “N” are bad enough (especially since on our system “Y” means “Yes it is OK” rather than “Yes there was an error” as I would expect), but “E” is even worse. It was an attempt to retrofit a third state onto a boolean field. Nasty.
Almost as bad as the brokenness of the design is the fact that the internal documentation of the system is now broken. error_ind is totally misleading and will almost certainly lead to developer misunderstandings as time goes on. The use of “Y” and “N” as values is also bad, since they are in such common use in databases that they are taken for granted as boolean true/false. Reusing a universal standard is just asking for trouble.
Agile practices that work against this anti-pattern are:
- constant refactoring — if you need a new column, add it, and even more importantly remove the old one. If something’s meaning has changed change the name.
- internal documentation — things should have meaningful names (I’ll definitely have more to say on this one - its one of my key practices).