• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why "Avoid inline conditionals"?

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author & internet detective
Posts: 42011
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic