Java Clean Code Tutorial #5 - Comparing String Constants

preview_player
Показать описание
Java String constant comparison is one of the most popular and heavily used java statements. I feel it’s very important that all developers are aware of this one simple tip when performing comparisons to avoid unexpected Null Pointer Exceptions.

In order to avoid null pointer exceptions, always place the constant first during the comparison.

For example, the graceful validation below will not throw a null pointer is the variable is null. The risk prone code will throw a null pointer exception if the variable is null.

Graceful validation - CONSTANT.equals(variable)

I feel it is important developers know about this idiom and when to apply it. It can drastically improves codebases with such small changes, unless you want the code to blow up! But usually i don't.

Share this quick tip with your team & fiends.

Software is written once and read hundreds of times. So let’s make our fellow developers life easier.

Why clean code?
Simple. Clean code allows us to confidently make changes and deliver more features quickly to our customers.

Don’t forget to subscribe for your regular dose of Java Tutorials!

STOP THE ROT.

Till episode 6,
Philip

Episode 5 of the free Java Clean Code Training Series.
Рекомендации по теме
Комментарии
Автор

hi can you post the full code and also show what happens when we run findbugs before and after you rectify the bugs in this code.

knightrider
Автор

wow, that was super cool :)
do you know any free tool for code review?
thank you

StefanBanu