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.