# Trade-off between variable name descriptiveness and readabil

**URL:** <https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429>\
**Category:** Ruby\
**Created:** [February 22, 2007, 6:32am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429 "2007-02-22T06:32:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [February 22, 2007, 6:32am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429/1 "2007-02-22T06:32:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [February 22, 2007, 7:05am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429/2 "2007-02-22T07:05:09Z")

</div>

Daniel F. wrote:

> > def sing\_song(start)  
> > start.downto(1) do |num|  
> > puts “#{num.bottles} of beer on the wall, #{num.bottles} of  
> > beer!”

I prefer num.bottles; it reads well.

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [February 22, 2007, 7:17am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429/3 "2007-02-22T07:17:38Z")

</div>

On Feb 22, 2007, at 12:32 AM, Daniel F. wrote:

> of beer on the wall"
> 
> > ```
> > puts "#{num.bottles} of beer on the wall, #{num.bottles} of beer!"
> > 
> > ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [February 22, 2007, 11:16am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429/4 "2007-02-22T11:16:08Z")

</div>

On Thu, 2007-02-22 at 14:32 +0900, Daniel F. wrote:

> beer on the wall"
> 
> > ```
> > puts
> > 
> > ```
> > 
> > end  
> > end

It should be “num\_bottles” if nothing else! 😉

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [February 22, 2007, 11:59am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429/5 "2007-02-22T11:59:15Z")

</div>

On Thu, 22 Feb 2007 06:32:26 +0100, Daniel F.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 V.

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [February 23, 2007, 3:20am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429/6 "2007-02-23T03:20:21Z")

</div>

On Feb 22, 4:58 am, “David V.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 V.

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.

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [February 23, 2007, 7:27am UTC](https://www.ruby-forum.com/t/trade-off-between-variable-name-descriptiveness-and-readabil/86429/7 "2007-02-23T07:27:37Z")

</div>

99 programmers will have 99 different solutions, so here’s mine 😉

BTW, I’m with David V.; make the loop index a short name and  
decorate it another way. I’ve tried at the same time to make the DRY  
concept sink in.

# Make the singer look a bit intelligent 🙂

def bottles(n)  
case n  
when 0  
“no more bottles”  
when 1  
“1 lonely bottle”  
else  
“#{n} bottles”  
end  
end

def sing\_song(starting\_bottles)  
starting\_bottles.downto(1) do |n|  
num\_bottles = bottles(n)  
puts “#{num\_bottles} of beer on the wall, #{num\_bottles} of beer!”  
num\_bottles = bottles(n - 1) # One down  
puts “Take one down, pass it around, #{num\_bottles} of beer on the  
wall.”  
puts  
end  
end

irb(main):047:0\> sing\_song 3  
3 bottles of beer on the wall, 3 bottles of beer!  
Take one down, pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer!  
Take one down, pass it around, 1 lonely bottle of beer on the wall.

1 lonely bottle of beer on the wall, 1 lonely bottle of beer!  
Take one down, pass it around, no more bottles of beer on the wall.

My $0.02.
