# Style question: if !x vs. unless x

**URL:** <https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584>\
**Category:** ruby-talk\
**Created:** [11 April 2006 18:04 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584 "2006-04-11T18:04:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Brady](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Brady](https://rubytalk.org/u/David_Brady)\
**Post date:** [11 April 2006 18:04 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/1 "2006-04-11T18:04:00Z")

</div>

Hi all,

One of the coolest things for me about learning ruby is that it's the first language I've learned in a decade where I actually had to learn a new language. Once you know C/C++, you can pick up Java, PHP, perl, Python, and even VB without really having to learn new grammar... you just learn where the braces and dollar signs go. One of the joys of ruby is that when I find myself using a klunky grammar construct, there is often an elegant solution if I will just surrender my C++ thinking style.

That paragraph could spark a thread of its own, but in this case, its lead-up for a simpler question. Which do you find more readable:

# this version  
if !correct  
&nbsp;&nbsp;handle\_error  
&nbsp;&nbsp;do\_some\_other\_thing  
end

# or this version  
unless correct  
&nbsp;&nbsp;handle\_error  
&nbsp;&nbsp;do\_some\_other\_thing  
end

When dealing with single-lines, I find the inverted construct to be MUCH more readable:

handle\_error unless correct

but when I have to make a block, my C++ instincts really prefer the "if !" version.

I'm just throwing this out here... what do you prefer, and why? Do you find the "unless x" construct as (or more) readable than the "if !x" construct? When I see a block beginning with unless, I really have to stop reading and tease out the logic. Is this a good instinct thing, or is it simply me lacking fluency in a common ruby idiom?

Hmm, I just discovered this syntax, and for small blocks, I prefer it greatly:

( handle\_error; do\_some\_other\_thing ) unless correct

Though I worry that ( ..., ... ) syntax may have some side effects I'm not expecting. That's list declaration syntax, isn't it? So using it as a block might bend POLS....

-dB

> **···**
>
> --  
> David Brady  
> [ruby\_talk@shinybit.com](mailto:ruby_talk@shinybit.com)  
> "Every normal man must be tempted, at times, to spit on his hands, hoist the Jolly Roger, and start slitting throats." -- H. L. Mencken

---

<div class="post-metadata">

**Author:** ![Jamie\_Macey](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Jamie\_Macey](https://rubytalk.org/u/Jamie_Macey)\
**Post date:** [11 April 2006 18:13 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/2 "2006-04-11T18:13:57Z")

</div>

Another version that works, but is a little confusing to read for  
anything larger than a handful of lines:

begin  
&nbsp;&nbsp;handle\_error  
&nbsp;&nbsp;do\_some\_other\_thing  
end unless correct

This is essentially just creating an anonymous scope (the begin/end)  
and appending the postcondition to that.

> **···**
>
> On 4/11/06, David Brady \<ruby\_talk@shinybit.com\> wrote:
> 
> > # this version  
> > if !correct  
> > &nbsp;&nbsp;handle\_error  
> > &nbsp;&nbsp;do\_some\_other\_thing  
> > end
> > 
> > # or this version  
> > unless correct  
> > &nbsp;&nbsp;handle\_error  
> > &nbsp;&nbsp;do\_some\_other\_thing  
> > end

---

<div class="post-metadata">

**Author:** ![Collins\_Justin](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Collins\_Justin](https://rubytalk.org/u/Collins_Justin)\
**Post date:** [11 April 2006 18:21 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/3 "2006-04-11T18:21:23Z")

</div>

David Brady wrote:

> Hi all,
> 
> One of the coolest things for me about learning ruby is that it's the first language I've learned in a decade where I actually had to learn a new language. Once you know C/C++, you can pick up Java, PHP, perl, Python, and even VB without really having to learn new grammar... you just learn where the braces and dollar signs go. One of the joys of ruby is that when I find myself using a klunky grammar construct, there is often an elegant solution if I will just surrender my C++ thinking style.
> 
> That paragraph could spark a thread of its own, but in this case, its lead-up for a simpler question. Which do you find more readable:
> 
> # this version  
> if !correct  
> handle\_error  
> do\_some\_other\_thing  
> end
> 
> # or this version  
> unless correct  
> handle\_error  
> do\_some\_other\_thing  
> end
> 
> When dealing with single-lines, I find the inverted construct to be MUCH more readable:
> 
> handle\_error unless correct
> 
> but when I have to make a block, my C++ instincts really prefer the "if !" version.
> 
> I'm just throwing this out here... what do you prefer, and why? Do you find the "unless x" construct as (or more) readable than the "if !x" construct?

\<snip\>

> -dB

Personally, I've been going with the 'unless' statement much more often, but it depends on the context. I generally use "unless" when I think the conditional is unusual and I normally would do what's in the block:

unless something\_is\_weird  
&nbsp;&nbsp;&nbsp;&nbsp;do stuff  
end

But that's just because I'm thinking about the normal meaning of 'unless'.  
I also use the 'word' versions of logical operators to make things more clear for myself:

if not correct  
&nbsp;&nbsp;&nbsp;&nbsp;#do something  
end

is very easy to follow. Just beware of the lower precedence of those constructs.  
This could be just me, though.

-Justin

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [12 April 2006 13:05 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/4 "2006-04-12T13:05:46Z")

</div>

> Hi all,
> 
> One of the coolest things for me about learning ruby is that it's the  
> first language I've learned in a decade where I actually had to learn a  
> new language. Once you know C/C++, you can pick up Java, PHP, perl,  
> Python, and even VB without really having to learn new grammar... you  
> just learn where the braces and dollar signs go. One of the joys of  
> ruby is that when I find myself using a klunky grammar construct, there  
> is often an elegant solution if I will just surrender my C++ thinking style.
> 
> That paragraph could spark a thread of its own, but in this case, its  
> lead-up for a simpler question. Which do you find more readable:
> 
> # this version  
> if !correct  
> &nbsp;&nbsp;handle\_error  
> &nbsp;&nbsp;do\_some\_other\_thing  
> end
> 
> # or this version  
> unless correct  
> &nbsp;&nbsp;handle\_error  
> &nbsp;&nbsp;do\_some\_other\_thing  
> end
> 
> When dealing with single-lines, I find the inverted construct to be MUCH  
> more readable:
> 
> handle\_error unless correct

In fact I'd usually make the above a single liner by using exceptions:

raise SomeError unless correct  
# continue normal processing

alternative

correct or raise SomeError  
# continue normal processing

> but when I have to make a block, my C++ instincts really prefer the "if  
> !" version.

I usually use "unless" if there is a single negated expression and use  
"if" otherwise. No distinction with regard to single line or block.

> Hmm, I just discovered this syntax, and for small blocks, I prefer it  
> greatly:
> 
> ( handle\_error; do\_some\_other\_thing ) unless correct

I don't like that. Use the normal if ... then .. end - that looks  
better, to me at least.

Kind regards

robert

> **···**
>
> 2006/4/11, David Brady \<ruby\_talk@shinybit.com\>:
> 
> --  
> Have a look: [Robert K. | Flickr](http://www.flickr.com/photos/fussel-foto/)

---

<div class="post-metadata">

**Author:** ![Charlie\_Bowman3](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Charlie\_Bowman3](https://rubytalk.org/u/Charlie_Bowman3)\
**Post date:** [11 April 2006 18:37 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/5 "2006-04-11T18:37:32Z")

</div>

I never like to start a line off with unless. It just looks confusing  
to me.

> **···**
>
> On Wed, 2006-04-12 at 03:21 +0900, Justin Collins wrote:
> 
> > David Brady wrote:  
> > \> Hi all,  
> > \>  
> > \> One of the coolest things for me about learning ruby is that it's the  
> > \> first language I've learned in a decade where I actually had to learn  
> > \> a new language. Once you know C/C++, you can pick up Java, PHP, perl,  
> > \> Python, and even VB without really having to learn new grammar... you  
> > \> just learn where the braces and dollar signs go. One of the joys of  
> > \> ruby is that when I find myself using a klunky grammar construct,  
> > \> there is often an elegant solution if I will just surrender my C++  
> > \> thinking style.  
> > \>  
> > \> That paragraph could spark a thread of its own, but in this case, its  
> > \> lead-up for a simpler question. Which do you find more readable:  
> > \>  
> > \> # this version  
> > \> if !correct  
> > \> handle\_error  
> > \> do\_some\_other\_thing  
> > \> end  
> > \>  
> > \> # or this version  
> > \> unless correct  
> > \> handle\_error  
> > \> do\_some\_other\_thing  
> > \> end  
> > \>  
> > \> When dealing with single-lines, I find the inverted construct to be  
> > \> MUCH more readable:  
> > \>  
> > \> handle\_error unless correct  
> > \>  
> > \> but when I have to make a block, my C++ instincts really prefer the  
> > \> "if !" version.  
> > \>  
> > \> I'm just throwing this out here... what do you prefer, and why? Do  
> > \> you find the "unless x" construct as (or more) readable than the "if  
> > \> !x" construct?  
> > \<snip\>  
> > \>  
> > \>  
> > \> -dB  
> > \>
> > 
> > Personally, I've been going with the 'unless' statement much more often,  
> > but it depends on the context. I generally use "unless" when I think the  
> > conditional is unusual and I normally would do what's in the block:
> > 
> > unless something\_is\_weird  
> > &nbsp;&nbsp;&nbsp;&nbsp;do stuff  
> > end
> > 
> > But that's just because I'm thinking about the normal meaning of 'unless'.  
> > I also use the 'word' versions of logical operators to make things more  
> > clear for myself:
> > 
> > if not correct  
> > &nbsp;&nbsp;&nbsp;&nbsp;#do something  
> > end
> > 
> > is very easy to follow. Just beware of the lower precedence of those  
> > constructs.  
> > This could be just me, though.
> > 
> > -Justin

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [11 April 2006 18:40 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/6 "2006-04-11T18:40:54Z")

</div>

Charlie Bowman wrote:

> I never like to start a line off with unless. It just looks confusing  
> to me.

Multi-conditional unless statements look confusing to me. But I am with Justin, depending on the context of the conditional may  
make it seem more approriate to do "unless" vs. "if not".

I tend to use both, although I no longer use multi-conditional unless statements, ever.

Zach

---

<div class="post-metadata">

**Author:** ![David\_Brady](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Brady](https://rubytalk.org/u/David_Brady)\
**Post date:** [12 April 2006 06:31 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/7 "2006-04-12T06:31:57Z")

</div>

zdennis wrote:

> I tend to use both, although I no longer use multi-conditional unless statements, ever.  
> &nbsp;&nbsp;

Interesting. You mean like this?

if x  
&nbsp;&nbsp;do\_something  
end unless y

I do like this syntax, though:

do\_something if condition unless exception

Oh, wait. By multiconditional unless, you mean stuff like this:

unless ( a || b || c )  
&nbsp;&nbsp;do\_something  
end

...yes?

-dB

> **···**
>
> --  
> David Brady  
> ruby\_talk@shinybit.com  
> "Every normal man must be tempted, at times, to spit on his hands, hoist the Jolly Roger, and start slitting throats." -- H. L. Mencken

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [12 April 2006 07:20 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/8 "2006-04-12T07:20:44Z")

</div>

I'm pretty sure he means:

unless condition  
&nbsp;&nbsp;...  
else  
&nbsp;&nbsp;&nbsp;..  
end

or

unless condition  
&nbsp;&nbsp;&nbsp;...  
elsif othercondition  
&nbsp;&nbsp;...  
else  
&nbsp;&nbsp;...  
end

> **···**
>
> On Apr 12, 2006, at 2:31 AM, David Brady wrote:
> 
> > Oh, wait. By multiconditional unless, you mean stuff like this:
> > 
> > unless ( a || b || c )  
> > do\_something  
> > end
> > 
> > ...yes?

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [12 April 2006 09:55 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/9 "2006-04-12T09:55:58Z")

</div>

Logan Capaldo wrote:

> > Oh, wait. By multiconditional unless, you mean stuff like this:
> > 
> > unless ( a || b || c )  
> > do\_something  
> > end
> > 
> > ...yes?
> 
> I'm pretty sure he means:
> 
> unless condition  
> ...  
> else  
> &nbsp;&nbsp;..  
> end
> 
> or
> 
> unless condition  
> &nbsp;&nbsp;...  
> elsif othercondition  
> ...  
> else  
> ...  
> end

I mean what Logan said plus the second half of what Dave said. I do not like the following, and I find it a) sometimes confusing  
to read and b) my brain has to think twice when reading, which is twice to much:

&nbsp;&nbsp;unless condition  
&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;elsif  
&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end

&nbsp;&nbsp;unless cond1 and cond2 and cond3  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end

&nbsp;&nbsp;unless (cond1 and cond2) or cond3  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end

&nbsp;&nbsp;do\_something unless cond1 and cond2

In regards to the first half of Dave's email:

> if x  
> do\_something  
> end unless y
> 
> I do like this syntax, though:
> 
> do\_something if condition unless exception

I have never thought of chaining conditionals like that, although the idea is very cool... and I find it readable in the case of  
error handling. (I wont say exception handling because IMO the two differ and exceptions should be handled by a different  
mechanism then what error conditions should be handled under, the whole 90/10 rule applies of course.) =)

Zach

> **···**
>
> > On Apr 12, 2006, at 2:31 AM, David Brady wrote:

---

<div class="post-metadata">

**Author:** ![Jon\_Bauman](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@Jon\_Bauman](https://rubytalk.org/u/Jon_Bauman)\
**Post date:** [12 April 2006 15:10 UTC](https://rubytalk.org/t/style-question-if-x-vs-unless-x/26584/10 "2006-04-12T15:10:27Z")

</div>

I don't think you can actually use elsif with an unless block:

$ irb --simple-prompt

> > unless true  
> > elsif true  
> > end

SyntaxError: compile error  
(irb):2: parse error, unexpected kELSIF  
elsif true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):3

> > if true  
> > elsif true  
> > end

=\> nil

Personally, I use unless any time my primary test is for a negated  
condition (and I don't need elsif). It takes a little while to get used  
to perhaps, but if you think about it as meaning "if not" (which is what  
unless actually means) it works. The think I like about using "unless x"  
as opposed to "if !x" or "if not x" is that there is no precedence to  
think about, it's totally clear that it negates the entire condition.

zdennis wrote:

> **···**
>
> > -----BEGIN PGP SIGNED MESSAGE-----  
> > Hash: SHA1
> > 
> > Logan Capaldo wrote:
> > 
> > > unless condition  
> > > &nbsp;&nbsp;...  
> > > elsif othercondition  
> > > ...  
> > > else  
> > > ...  
> > > end
> > 
> > I mean what Logan said plus the second half of what Dave said. I do not  
> > like the following, and I find it a) sometimes confusing  
> > to read and b) my brain has to think twice when reading, which is twice  
> > to much:
> > 
> > &nbsp;&nbsp;unless condition  
> > &nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;elsif  
> > &nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;unless cond1 and cond2 and cond3  
> > &nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;unless (cond1 and cond2) or cond3  
> > &nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;do\_something unless cond1 and cond2
> > 
> > In regards to the first half of Dave's email:
> > 
> > > if x  
> > > do\_something  
> > > end unless y
> > > 
> > > I do like this syntax, though:
> > > 
> > > do\_something if condition unless exception
> > 
> > I have never thought of chaining conditionals like that, although the  
> > idea is very cool... and I find it readable in the case of  
> > error handling. (I wont say exception handling because IMO the two  
> > differ and exceptions should be handled by a different  
> > mechanism then what error conditions should be handled under, the whole  
> > 90/10 rule applies of course.) =)
> > 
> > Zach
> > 
> > -----BEGIN PGP SIGNATURE-----  
> > Version: GnuPG v1.4.1 (GNU/Linux)  
> > Comment: Using GnuPG with Thunderbird - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)
> > 
> > iD8DBQFEPM/LMyx0fW1d8G0RAvJgAJwKfktuwgUbcI6iZ+Nd0mVzqxoq5gCeJhoX  
> > f7Ygn98m+JdYMPB5ZkrvL4c=  
> > =h6ey  
> > -----END PGP SIGNATURE-----
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
