From: David Vallner Date: 2007-02-22T19:58:41+09:00 Subject: Re: Trade-off between variable name descriptiveness and readability On Thu, 22 Feb 2007 06:32:26 +0100, Daniel Finnie wrote: > > 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. > 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