Article FX28 Coded Smorgasbord: If You Want To

Coded Smorgasbord: If You Want To

by
Remy Porter
from The Daily WTF on (#FX28)

We pick on date handling code a lot here, simply because there are so many ways to mess up date related code (because dates are hard). In a way, it's almost like we're cheating. Even smart programmers could mess that up. What about basic conditional logic? How hard could that be to mess up?

Well Jan L. came across this solution to a simple boundary check- if telegramType is between 100 and 199, it is a payment type telegram.

boolean isPaymentType = false;for (int i = 100; i < 199; i++) { if (telegramType == i) { isPaymentType = true; }}

If it looks stupid but it works it's" no, it's still stupid.

Well, what about when you're getting a string, and you need to see if it's "true"? You could do a raw string comparison, or even better, convert the string to a boolean type using a built-in function. Or, you could be like **Marcus M.'s``` co-worker and do this:

Private Function thisWasARealFunctionFoundInACodeBase() As Boolean Return (MyCBPage.MyNeo.GetQSVar("contenton").ToLower() = "true" Or MyCBPage.MyNeo.GetQSVar("contenton").ToLower() = "true" Or MyCBPage.MyNeo.GetQSVar("contenton").ToLower() = "true")End Function

For those keeping score at home, that is the same expression tested three times. I assume this function started life before the developer learned about ToLower, and they intended to cycle through every possible capitalization of "tRuE".

Well, at least none of these are trying to parse a bit mask using a triple-nested ternary expression hidden behind a C++ macro, right?

Chris sends us this:

#define CALCOPT_NOHITS ( (m_CalcOpt & (CALCOPT_TRACEHITS|CALCOPT_TRACESUBHITS)) == (CALCOPT_TRACEHITS|CALCOPT_TRACESUBHITS) ? m_CalcOpt^(CALCOPT_TRACEHITS|CALCOPT_TRACESUBHITS) : ((m_CalcOpt&CALCOPT_TRACEHITS) ? (m_CalcOpt^CALCOPT_TRACEHITS) : (m_CalcOpt&CALCOPT_TRACESUBHITS)?(m_CalcOpt^CALCOPT_TRACESUBHITS):m_CalcOpt) )

Chris replaced that with a far simpler macro (that doesn't use a ternary): #define CALCOPT_NOHITS (m_CalcOpt & ~(CALCOPT_TRACEHITS | CALCOPT_TRACESUBHITS))

Alright, so maybe conditional logic is hard. But you know what should be easy? Embedding a script in a web page. I mean, there's built-in, simple tags for that. How hard could that be?

Olivier V.'s company hired a 3rd party web consultancy that came up with this:

document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + url + '"></sc'+'ript>');

I can only assume that someone in their organization banned the use of the word "script" inside of a script.

puppetlabs50.png[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today! TheDailyWtf?d=yIl2AUoC8zAOihX7h7NQ9s
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