Showing posts with label booleans. Show all posts
Showing posts with label booleans. Show all posts

Thursday, 9 January 2020

The Pitfalls of Boolean Trap

A colleague is looking to update all inspections in IntelliJ at work, which is commendable.

He referred a link, see [1], to me regarding the use of Booleans as method parameters ("switches"), which is a pet peeve of another colleague of mine.

I thought I'd just mention it here, for who's interested.

References

[1] The Pitfalls of Boolean Trap
https://ariya.io/2011/08/hall-of-api-shame-boolean-trap

Thursday, 26 May 2016

Replacing ... With Boolean Expression

I found my IDE (IntelliJ) mentioning casually to me that I should replace this:
return municipality != null ? countSupply(municipality) > 0 : false;
With this:
return municipality != null && countSupply(municipality) > 0;

Trivial, I know, but most impressive of the IDE to catch that.

In the mean time, I'll patiently wait for the next version of the IDE that can actually do my programming for me.

Until that day, I'd better get back to work.

Thursday, 10 December 2015

true vs. Boolean.TRUE

I saw the following code below at my work, and it got me thoroughly confused. I found the statement above to be unnecessarily complicated.

private boolean useAsReferenceModel = Boolean.FALSE;

Boolean.FALSE returns a Boolean, which is a wrapper2 around the boolean primitive types.

So, in the example above, the Boolean wrapper is auto-unboxed3 to the primitive type false.

So, I changed the code to the simple and straightforward:
private boolean useAsReferenceModel = false;
Let us not make the world more complicated than it really is.

References

[1] StackOverflow - Java what is the difference between false and Boolean.FALSE
http://stackoverflow.com/questions/20090306/java-what-is-the-difference-between-false-and-boolean-false
[2] Wikipedia - Primitive Wrapper Classes
https://en.wikipedia.org/wiki/Primitive_wrapper_class
[3] Oracle JavaDoc - Autoboxing and unboxing
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html