Article 698W3 CodeSOD: Tenrning Nulls into Values

CodeSOD: Tenrning Nulls into Values

by
Remy Porter
from The Daily WTF on (#698W3)

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.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more.
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