• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why "Avoid inline conditionals"?  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've recently started using Checkstyle set to use the Sun Coding standards. However, one of the messages I'm receiving is as follows:

- Avoid inline conditionals.

It is reporting it on the following line:

All this does is return an int from an ArrayList where Integer was used to store the int in the List.

I thought the line of code was an easy way to check for a null object. Could someone explain what the significance of "Avoid inline conditionals" is and how I should update my line of code, if its deemed to be not to standard?

Cheers,
Matt.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, of course you could write



That's the problem with automated style checkers: either you surrender to their recommendations, or you learn to live with ignoring some of the warnings.

I don't know about this particular checker, but it's possible that this form would make it complain about multiple return statements (another very dubious criticism, in my view.) Then you'd have to write



to shut that one up, too!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for avoiding inline conditionals is to minimize confusing code. As Ernest points out, it isn't confusing in this case.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing coming to my mind that could improve the code is removing the need for the null check at all - for example by applying the Null Object pattern.

But I use inline conditionals all the time - they often make the code much clearer, if used wisely. I'd advise to disable this specific check, it's silly.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I'll switch off the setting for this particular check. I just wanted to make sure I wasn't missing something.

I like the Null Object Pattern, but for this piece of code I think it would be overkill to implement it.

Cheers,
Matt.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!