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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With