Article 3NE9P Representative Line: The Truth About Comparisons

Representative Line: The Truth About Comparisons

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

We often point to dates as one of the example data types which is so complicated that most developers can't understand them. This is unfair, as pretty much every data type has weird quirks and edge cases which make for unexpected behaviors. Floating point rounding, integer overflows and underflows, various types of string representation"

But file-not-founds excepted, people have to understand Booleans, right?

Of course not. We've all seen code like:

if (booleanFunction() == true) "

Or:

if (booleanValue == true) { return true;} else { return false;}

Someone doesn't understand what booleans are for, or perhaps what the return statement is for. But Paul T sends us a representative line which constitutes a new twist on an old chestnut.

if ( Boolean.TRUE.equals(functionReturningBooleat(pa, isUpdateNetRevenue)) ) "

This is the second most Peak Java Way to test if a value is true. The Peak Java version, of course, would be to use an AbstractComparatorFactoryFactory<Boolean> to construct a Comparator instance with an injected EqualityComparison object. But this is pretty close- take the Boolean.TRUE constant, use the inherited equals method on all objects, which means transparently boxing the boolean returned from the function into an object type, and then executing the comparison.

The if (boolean == true) return true; pattern is my personal nails-on-the-chalkboard code block. It's not awful, it just makes me cringe. Paul's submission is like an angle-grinder on a chalkboard.

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=yIl2AUoC8zAVtyx7YCR-hk
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