A former co-worker of David S wanted to check for nulls, and apparently, they had just learned about the ternary operator, so they wanted to combine these actions. That, itself, isn’t a WTF- using ternaries to coalesce nulls is a time-honored tradition and generally pretty effective.

Let’s see how this Java developer approached it:

return findProgram(
    id.isEmpty() ? null : id, 
    title == null ? null : title, 
    start == null ? null : start, 
    end == null ? null : end);

The first parameter actually makes sense to me, or at least isn’t the worst thing. I’m not fond of passing nulls around (optional types are almost always a better choice), but I don’t hate it.

Then we get the delightful, pointless checks that make this a true WTF. If title is null, pass a null, otherwise pass title. If start is null, pass null, otherwise pass start. And so on.

But hey, at least they got to use ternary expressions.

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

Remy Porter

Source link

You May Also Like

Indonesia Bans Sex Outside Of Marriage

Indonesia’s parliament has approved a new criminal code that bans anyone in…

A Little Too Ripped? | People Of Walmart

The post A Little Too Ripped? first appeared on People Of Walmart.…

Mildly Infuriating Pictures That You Can Actually Feel

These mildly infuriating things happen almost every day… Pushing you closer and…

An Explosion of Strings

Rich was digging through some legacy PHP code, trying to track down…