wood burning stoves 2.0*
The moose likes Cattle Drive and the fly likes when to use not (!) with else Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "when to use not (!) with else" Watch "when to use not (!) with else" New topic
Author

when to use not (!) with else

Sam Hume
Greenhorn

Joined: Nov 07, 2006
Posts: 4
A nitpick reads something along the lines of: Avoid using a not (!) and an else in the same statement...Test for == and swap your if and else blocks.

Now, I'm aware of this rule of thumb, but I've also heard that it's better to put the expected result first and the exception in the else block. So, in the case of checking for failed method I might do something like:



In this case the normal and expected result of my method is to return a non-null result and keep on truckin'.

So my question is: which is easier to read?
1) eliminate the not (!) and swap the if and else blocks
or
2) keep the not (!) and put the expected/normal case first (as I've shown in the example)

And, are there any cases where using not (!) and else together makes sense from a readability standpoint?

Cheers,
Sam
M Easter
Ranch Hand

Joined: Feb 11, 2007
Posts: 133
I agree with your questioning of the nit-pick.

IMHO, it is best to have the "happy case" in the if block and the "unusual case" in the else... and let the logical expression be whatever it happens to be.


M Easter
Software Composer - http://codetojoy.blogspot.com
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20660
    ∞

Another way to look at it is that you are getting the off case out of the way up front.

This is a topic that can be at the heart of some mild debate. Kinda like "top down" vs. "bottom up."

Since the camps are generally evenly divided about "do happy path first" vs. "get the odd ducks out of the way first", and everybody can agree on simplifying code ... I think that what your nitpicker is suggesting is generally the best route.

Make no mistake - this is more of a guideline than an absolute. Readability is always king. And there are limitations to the english language.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Sam Hume
Greenhorn

Joined: Nov 07, 2006
Posts: 4
Since the camps are generally evenly divided... I think that what your nitpicker is suggesting is generally the best route.

No doubt. I followed the nitpicker's guidance assuming there's more wisdom to be found down that path. But this is a nitpick I wanted to explore a little, rather than simply complying and moving on.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9050
    
  10
pseudocode:
if the methodResult is not null, continueSinceMethodSucceeded.
if the methodResult is not not null, failDueToUnexpectedResult

I think this has the potential to be confusing.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use not (!) with else