Article 3NQBK CodeSOD: If Not Null…

CodeSOD: If Not Null…

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

Robert needed to fetch some details about pump configurations from the backend. The API was poorly documented, but there were other places in the code which did that, so a quick search found this block:

var getConfiguration = function(){ .... var result = null; result = getPumpConfiguration (areaID,subStationID,mngmtUnitID,lastServiceDate,service,format,result); result = getPumpConfiguration (areaID,subStationID,null,lastServiceDate,null,format,result); result = getPumpConfiguration (areaID,subStationID,null,lastServiceDate,service,null,result); result = getPumpConfiguration (areaID,subStationID,mngmtUnitID,lastServiceDate,null,null,result); result = getPumpConfiguration (areaID,subStationID,null,lastServiceDate,null,null,result); return result;}

This collection of lines lurked at the end of a 100+ line function, which did a dozen other things. At a glance, it's mildly perplexing. I can see that result gets passed into the function multiple times, so perhaps this is an attempt at a fluent API? So this series of calls awkwardly fetches the data that's required? The parameters vary a little with every call, so that must be it, right?

Let's check the implementation of getPumpConfiguration"

function getPumpConfiguration (areaID,subStationID,mngmtUnitID,lastServiceDate,service,format,result) { if (result==null) { ... result = queryResult; ... } return result;}

Oh, no. If the result parameter has a value" we just return it. Otherwise, we attempt to fetch data. This isn't a fluent API which loads multiple pieces of data with separate requests, it's an attempt at implementing retries. Hopefully one of those calls works.

otter-icon.png [Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today! TheDailyWtf?d=yIl2AUoC8zA5LEEJkCa71I
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