The magic strings anti-pattern implies a much more entertaining experience than it is.  This coding style is not rare.  Thus, it is likely you have encountered it or even fallen into this trap.  As always, proven design principles and best practices help.  However, the reasons this is an anti-pattern are worth reviewing.

Defining the Magic Strings Anti-Pattern

The DevIQ definition for the magic strings anti-pattern provides an excellent discussion of the cons of this approach. [Click Here to See The Page]

“Magic strings are string values that are specified directly within application code that have an impact on the application’s behavior. Frequently, such strings will end up being duplicated within the system, and since they cannot automatically be updated using refactoring tools, they become a common source of bugs when changes are made to some strings but not others. 

https://rb-sns.com

The first sentence gives us a brief definition, and I like how it dives right into the problems this causes.  The challenge with this anti-pattern is that it is so easy to fall into it.  There are some reasons we might think that we can get away with a magic number.  It is best to avoid these temptations.  The sanity you save may be your own.

Clever Is Not A Best Practice

I have had arguments over the years about magic numbers.  There are all sorts of discussions about speed, performance, clever solutions, and more.  The thing is, none of that matters.  The anti-pattern can drive you nuts and reduce quality dramatically. 

Consider a situation where you have to change a magic number.  You have no way to determine how often or where it is used in the code.  That alone could lead to weeks or months of tracking down outlier bugs caused by the number change.  Even worse, the update could lead to data corruption if you miss a couple of usages in the code.  Always fall on the side of more straightforward code over saving a process cycle or two.

Fixes Are Hard

The example provided above is also why this is so hard to recover from.  First, you have to realize there are magic numbers in use.  Then, you have to track down where they are used.  You might even have to figure out what they mean and why they are there.  Nothing good can come from that.

We have looked at a lot of anti-patterns.  However, this may be the most insidious one yet.  It is incredibly easy to fall into and almost impossible to correct.

Business Category (English) 468x60

Rob Broadhead

Rob is a founder of, and frequent contributor to, Develpreneur. This includes the Building Better Developers podcast. He is also a longtime student of technology as a developer, designer, and manager of software solutions. Rob is a founder and principle of RB Consulting and has managed to author a book about his family experiences. In his free time, he stays busy raising five children (although a few have grown into adults). When he has a chance to breathe, he is on the ice playing hockey to relax.

Leave a Reply