Local Variable Names: Whole Words Versus Letters

I’m trying to make sure I really understand Ruby’s blocks and closures, so I was digging ahead to Chapter 22 of the Pickaxe book on ‘Blocks, Closures, and Proc Objects.’ On page 358-359 there was a bit of example code:

def meth1
  (1..10).each do |val|
    return val  # returns from method
  end
end

I would have just used

1
value

for the variable name, but maybe this is a very popular idiom in Ruby.

t = Thread.new do

OK, this is the sort of stuff that drives me crazy in Java when people do it. Instead of just naming it thread or something logical we just assign it to

1
t

, a meaningless English character. When you read later down in the code you get no hint about what

1
t

is so you have to hold that in your brain or refer back to its definition again.

Then there’s there’s these examples :

pr = Proc.new { next 99 }
p = Proc.new { return 99 }

Couldn’t it have been at least

1
proc

! At this point I don’t grok enough about ruby’s Proc class to really come up with a better name, but it appears to be a closure so that might be a reasonable name as well.

I know the counter arguments. One, this is a book with example code so they’re trying to save space. Possible, but you’re also introducing the language to people so you should probably try to be as clear as possible. Two, it saves typing. True, but typing is not what slows down my coding, defects and harder to follow code does. Three, that’s the Ruby idiom. That may be very true, so this may seem as normal to me as using i,j for looping counters. I’ll just have to see if that proves out.

Technorati Tags:

,