Trade-off between variable name descriptiveness and readability

D

Daniel Finnie

As a mentor of the new adopt-a-newbie system, which I think is going
excellently, I was reviewing a 99-bottles-of-beer method. We went over
what he had, what I thought could be better and I showed an example of
exactly how I would do it, which was this:
def sing_song(start)
start.downto(1) do |numBottles|
puts "#{numBottles.bottles} of beer on the wall,
#{numBottles.bottles} of beer!"
puts "Take one down, pass it around, #{(numBottles-1).bottles} of beer on the wall"
puts
end
end

String#bottles just does "x bottle(s if needed)." Anyway, one could
further refine this code to be more english-y by changing numBottles to
num in the following way:
def sing_song(start)
start.downto(1) do |num|
puts "#{num.bottles} of beer on the wall, #{num.bottles} of beer!"
puts "Take one down, pass it around, #{(num-1).bottles} of beer on the wall"
puts
end
end

I doing this, the code reads better but the variable's name loses a lot
of its descriptiveness. What would all of you guys do? I was also
thinking of renaming the bottles method to disp, but that has the
potential to conflict with a lot of other things and doesn't really
describe what the method does. I also considered putting
numBottles.bottles into a variable but it didn't seem right.

Dan
 
C

Craig Demyanovich

As a mentor of the new adopt-a-newbie system, which I think is
going excellently, I was reviewing a 99-bottles-of-beer method. We
went over what he had, what I thought could be better and I showed
an example of exactly how I would do it, which was this:


String#bottles just does "x bottle(s if needed)." Anyway, one
could further refine this code to be more english-y by changing
numBottles to num in the following way:


I doing this, the code reads better but the variable's name loses a
lot of its descriptiveness. What would all of you guys do? I was
also thinking of renaming the bottles method to disp, but that has
the potential to conflict with a lot of other things and doesn't
really describe what the method does. I also considered putting
numBottles.bottles into a variable but it didn't seem right.

The second method above reads a little better to me than the first;
numBottles.bottles is repetitive. However, I don't like "start," and
I'd like to throw out "how_many" in place of "num":

def sing_song(bottles_on_the_wall)
bottles_on_the_wall.downto(1).do | how_many |
puts "#{how_many.bottles} of beer on the wall, #
{how_many.bottles} of beer!"
puts "Take one down, pass it around, #{(how_many - 1).bottles}
of beer on the wall"
puts
end
end

What do you think?

Craig
 
D

Daniel Schierbeck

As a mentor of the new adopt-a-newbie system, which I think is going
excellently, I was reviewing a 99-bottles-of-beer method. We went over
what he had, what I thought could be better and I showed an example of
exactly how I would do it, which was this:

#{numBottles.bottles} of beer!"

It should be "num_bottles" if nothing else! ;)


Cheers,
Daniel
 
D

David Vallner

I doing this, the code reads better but the variable's name loses a lot
of its descriptiveness. What would all of you guys do? I was also
thinking of renaming the bottles method to disp, but that has the
potential to conflict with a lot of other things and doesn't really
describe what the method does. I also considered putting
numBottles.bottles into a variable but it didn't seem right.

Quoth Mr. Torvalds: 'If you have some random integer loop counter, it
should probably be called "i". Calling it "loop_counter" is
non-productive, if there is no chance of it being mis-understood.'

I'd go with that, and not putting a #bottles method into Integer - I find
that more or less pointless. (Also, I consider open classes and use and
abuse thereof an advanced topic that's better treated along with some
design.) That would make it rather abundantly clear from looking one line
below what 'i' means, and I'll submit that a newbie that can't work with
at least sufficiently straightforward (i.e. no method chain of five
iterators) three lines of code in his head is beyond hope anyway.

David Vallner
 
C

CHubas

Quoth Mr. Torvalds: 'If you have some random integer loop counter, it
should probably be called "i". Calling it "loop_counter" is
non-productive, if there is no chance of it being mis-understood.'

I'd go with that, and not putting a #bottles method into Integer - I find
that more or less pointless.
David Vallner

For actual classes, I should agree, but to teach a newbie, I'd go with
num.bottles or num_bottles, as they're more explanative to keep track
of where did they come from, and what should they do.

Also, the implementation of the method depends on what are you
pretending to show (assuming it's for teaching). If you want to show
the power of OO, I'd stick a #bottles method onto Fixnum; if you want
to show blocks, I'd go with a method that yields the number of bottles
(or the string "No more"); I think in this case, you should give more
than one option. For example, another way would be.

class Wall

attr_accessor :bottles

def initialize(bottles)
@bottles = bottles
end

def to_s
"the wall"
end

def sing
"#{@bottles} of beer on #{self} .....

etc...

and analyze which solution is the best. A good exercise for your
traineé wold be select the best approach and state why it is so.

Just sharing my point of view.

Ruben.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top