Article 3Q86N CodeSOD: A Bit Masked

CodeSOD: A Bit Masked

by
Remy Porter
from The Daily WTF on (#3Q86N)

The "for-case" or "loop-switch" anti-pattern creates some hard to maintain code. You know the drill: the first time through the loop, do one step, the next time through the loop, do a different step. It's known as the "Anti-Duff's Device", which is a good contrast: Duff's Device is a clever way to unroll a loop and turn it into a sequential process, while the "loop-switch" takes a sequential process and turns it into a loop.

Ashlea inherited an MFC application. It was worked on by a number of developers in Germany, some of which used English to name identifiers, some which used German, creating a new language called "Deunglish". Or "Engleutch"? Whatever you call it, Ashlea has helpfully translated all the identifiers into English for us.

Buried deep in a thousand-line "do everything" method, there's this block:

if(IS_SOMEFORMATNAME()) //Mantis 24426{ if(IS_CONDITION("RELEASE_4")) { m_BAR.m_TLC_FIELDS.DISTRIBUTIONCHANNEL=""; CString strKey; for (unsigned int i=1; i<16; i++) // Test all combinations { strKey="#W#X#Y#Z"; if(i & 1) strKey.Replace("#W", m_strActualPromo); // MANTIS 45587: Search with and without promotion code if(i & 2) strKey.Replace("#X",m_BAR.m_TLC_FIELDS.OBJECTCODE); if(i & 4) strKey.Replace("#Y",TOKEN(strFoo,H_BAZCODE)); if(i & 8) strKey.Replace("#Z",TOKEN(strFoo,H_CHAIN)); strKey.Replace("#W",""); strKey.Replace("#X",""); strKey.Replace("#Y",""); strKey.Replace("#Z",""); if(m_lDistributionchannel.GetFirst(strKey)) { m_BAR.m_TLC_FIELDS.DISTRIBUTIONCHANNEL="R"; break; } } } else m_BAR.m_TLC_FIELDS.DISTRIBUTIONCHANNEL=m_lDistributionchannel.GetFirstLine(m_BAR.m_TLC_FIELDS.OBJECTCODE+m_strActualPromo);}

Here, we see a rather unique approach to using a for-case- by using bitmasks to combine steps on each iteration of the loop. From what I can tell, they have four things which can combine to make an identifier, but might get combined in many different ways. So they try every possible combination, and if it exists, they can set the DISTRIBUTIONCHANNEL field.

That's ugly and awful, and certainly a WTF, but honestly, that's not what leapt out to me. It was this line:

if(IS_CONDITION("RELEASE_4"))

It's quite clear that, as new versions of the software were released, they needed to control which features were enabled and which weren't. This is probably related to a database, and thus the database may or may not be upgraded to the same release version as the code. So scattered throughtout the code are checks like this, which enable blocks of code at runtime based on which versions match with these flags.

Debugging that must be a joy.

raygun50.png [Advertisement] Forget logs. Next time you're struggling to replicate error, crash and performance issues in your apps - Think Raygun! Installs in minutes. Learn more. TheDailyWtf?d=yIl2AUoC8zAMb6S1cE2HFo
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments