On Sun, 7 Oct 2007 03:44:20 +0900, Greg Willits wrote:

> Ah, OK. Strange structure. (rhetorical Q:) Why read through all the 
> do_stuff if it isn't going to matter by the time you get the IF?

Rhetorical semi-answer (by argument to consequences):

Ruby, unlike some (most?) languages, *always* requires an end for its if
statements; that is to say, you can't just type

  if debug_mode then
    puts "Got to line 123"

  do_something important

You have to have an "end" for the if.

Allowing the form

  puts "Got to line 123" if debug_mode
  do_something important

lets you have more compact code, at the potential expense of readability.
I believe the construct came from Perl, but I'm not sure that's the first
place it ever appeared.

For readability's sake, I usually follow the guideline "don't use that form
unless the main body of code is the common path".  That is, I wouldn't use
it for debug statements, because they're usually not executed, so you spend
a lot of time reading code and then realizing it didn't get executed after
all.  I might use it for something like:

  puts "First name: #{first_name}" unless first_name.nil?

where the "unless" is more of a guard statement.  


-- 
Jay Levitt                |
Boston, MA                | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://www.jay.fm         | - Kristoffer