Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it considered acceptable to have no comments in good Ruby code? [closed]

I reviewed some professional code written in Ruby and found no comments. The code was reasonably clear to read but not self-documenting. Should I expect professionally written Ruby code to have comments? Or, is there some Ruby doctrine that comments are not considered essential?

like image 482
mikera Avatar asked Jul 07 '11 10:07

mikera


People also ask

How do you comment out a block of code in Ruby?

The Ruby single-line comment begins with the # character and ends at the end of the line. Any characters from the # character to the end of the line are completely ignored by the Ruby interpreter.

How do you comment in rails?

The comment starts with a pound ( # ) symbol. We put a space between the contents of the comment & the start of the comment to make it easier to read. Everything inside the comment is not interpreted as code, even if it looks like code. This means you can temporarily comment out code to disable it.


2 Answers

This issue isn't unique to Ruby.

Code comments should be kept to an absolute minimum, because they are usually not updated when the code changes, and become more misleading than helpful.

As you have already suggested, the best code is self-documenting and requires no comments.

Edit: to clarify, if you cannot reduce the code to remove the complexity, you must provide comments. This is rare in my experience, and usually only applies when external components don't behave as you might expect.

like image 84
James L Avatar answered Sep 28 '22 00:09

James L


Matz, the creator of Ruby, states his philosophy here and there: "The source code is the documentation. It even states all the bugs correctly." And he probably means that for comments as well. I think many people who don't comment in Ruby source code are following his words. Whether for you to follow him is up to you.

I have also read some Ruby introductory websites saying that, whenever you feel the necessity to comment, that is an indication that you should split that routine as in individual method, and name it as you were to comment that part.

like image 24
sawa Avatar answered Sep 28 '22 01:09

sawa