The difference is a matter of programming style. It makes it clear
that the comment is attached to the attribute. Also it keeps someone
from doing this
this is an X comment
a = b * c
puts b.to_s
attr_reader :x
… sticking code between the comment and the object it belongs to.
On Feb 15, 2008, at 5:28 PM, Jeremy McAnally wrote:
attr_accessor :forty_two
Yuck, that is 8 lines vs. 3 (below). I always found it
strange that RDOC didn’t parse comments to the right
of an accessor declaration yet that is exactly how
attributes are documented in the HTML pages generated
by RDOC.
attr_reader :fun # This does something fun
attr_accessor :read_write # This does something writable
attr_accessor :forty_two # This does something AWESOME
Readability is in the eye of the beholder. To me, good inline
documentation is far more useful than long preambles because it’s
right next to (or on top of) the relevant code. Classes and methods
that are prefaced by a ton of documentation feel like PHPDoc to me.
I’ll have to agree with Gary on this one but not just because of LOC
– because the proximity of the documentation to the code makes it
more relevant. It also makes it more likely that I’ll change the
comment if I change the code.
attr_accessor :read_write
attr_reader :fun # This does something fun
attr_accessor :read_write # This does something writable
attr_accessor :forty_two # This does something AWESOME
Well, if you take a look at vendor/rails/railties/lib/code_statistics.rb
while line = f.gets
stats["lines"] += 1
stats["classes"] += 1 if line =~ /class [A-Z]/
stats["methods"] += 1 if line =~ /def [a-z]/
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^
\s*#/
end
You’ll see that both forms will be counted as 3 LOC since the blank
and comment-only lines are not counted as ‘codelines’.