From: Charlie Bowman Date: 2006-04-12T03:37:32+09:00 Subject: Re: Style question: if !x vs. unless x --=-ShwUFFUZPlVfchIm2tGv Content-Type: text/plain Content-Transfer-Encoding: 7bit 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? > > > > > > > -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 > 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 > #do something > end > > is very easy to follow. Just beware of the lower precedence of those > constructs. > This could be just me, though. > > -Justin > > --=-ShwUFFUZPlVfchIm2tGv--