Conditionals are a constant source of problems for some developers. Russell F inherited some code which needed to take a list of parts and filter the list of parts down to something which customers could actually order.
The specific rule was that they needed to include only parts that were: not in stock and not in close out, in stock and not in close out, or in close out but also available. Which, given that business rule, that's exactly what the developer implemented:
var filtered = lstParts.Where(part => (!part.Stock && !part.CloseOut) || (part.Stock && !part.CloseOut) || (part.CloseOut && part.IsAvail == true));
The fact that these are all boolean values, and most of the time the developer understood that makes the closing
part.IsAvail == true line extra painful. But, reading both the rule and the code, you might realize that, like a middle school word problem, there's some red herrings and irrelevant details.
Russell made a much more concise version of the code by thinking logically about the statement:
var filtered = lstParts.Where(part => part.IsAvail || !part.CloseOut);
But that's not the entirety of the WTF, here. Russell has a few other details to add.
Note: I changed some names here for clarity's sake; for instance, the list wasn't actually called lstParts. I also changed the capitalization of some properties because we have some very unusual rules for casing (properties that correspond to actual database fields are for some reason capitalized differently than properties that are calculated on object initialization, for instance) and I didn't want to get side-tracked into a discussion of those rules.
That sounds less like a side track and more like a WTF of its own.
Also, Russell has another aside worth mentioning. If you're anything like me, you've had the experience where you search for something, find a Stack Overflow question and see exactly what you need- and also discover that you either asked the question or posted the answer five years ago. Well, Russell has a… different version of that experience.
A few days ago I was assigned to a task where I couldn't remember exactly where the code was that I'd need to look at. However, I did remember that I'd submitted some of it to TDWTF, so I did a search of your site for my submissions to find it (it was A Ritual Approach).
This post originally appeared on The Daily WTF.