Article 6B4G8 CodeSOD: Dining on Leftovers

CodeSOD: Dining on Leftovers

by
Remy Porter
from The Daily WTF on (#6B4G8)

Sometimes, you go up to the fridge, looking for a snack, and see the array of leftovers, and your appetite fails you. You want to eat, you know you have plenty of options that you just need to throw into the microwave, but none of them look appetizing. Leftovers always seem like a better idea when you're busy making them than when you're trying to deal with them after the fact.

Stev's co-worker has left them a large pile of leftovers. This block is representative of all the half-finished leftovers that don't do anything and shouldn't be there, but happily make it much harder to understand the codebase:

CString AddBcdStrings(CString LeftText, CString RightText){ CString Result = "";#ifdef DO_ALL // check both hex strings if (IsHexData(LeftText) && IsHexData(RightText)) { // convert to __int64 __int64 Left = ConvertBcdStrToInt64(LeftText); __int64 Right = ConvertBcdStrToInt64(RightText); // modify left to include carry provision Left += 0x0666666666666666; }#endif return (Result);}

As you can see, the bulk of this method is protected by a compile time guard. Let's pretend it isn't there, for the moment, though, and trace through the code.

We create an empty string to hold the result. We then check if LeftText and RightText are hex strings, and if they are, we parse them into integers, then add a large hex value to Left to "include carry provision". Then we discard all that work and return the empty string as a result.

DO_ALL, by the way, is not now and never was defined anywhere in the codebase. Which, if you think about it, is a rather clever optimization to remove unnecessary operations from a function whose clear purpose is to return an empty string.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more.
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