DEV Community

Cover image for 49 Days of Ruby: Day 44 -- Code Comments
Ben Greenberg
Ben Greenberg

Posted on

49 Days of Ruby: Day 44 -- Code Comments

Welcome to day 44 of the 49 Days of Ruby! πŸŽ‰

We are only a few days away from finishing our 49-day journey through Ruby! We took a quick detour yesterday to discuss version control, and today, we are going to discuss commenting on code.

Commenting on your code is a very important, and often underappreciated, feature of software development.

Code comments can not only help others trying to understand your application, but they can even help you!

How can code comments help you with your own code?

Let's say you struggled to try to get something right after days and days of work. You eventually end up with a solution. The solution is complex and a bit cryptic.

Now, a couple of months have passed, and you return back to that code. How hard will it be for you to get back into it and remember what you did there?

With code comments, it could be a lot easier!

How do you write comments in your Ruby code?

# This is a comment
# This is another comment
def my_method
  puts "here is some code"
  puts "this code is really complex"
  # I'm adding a comment in the middle of the method
end
Enter fullscreen mode Exit fullscreen mode

The # symbol tells Ruby not to interpret what follows it on that line. You can interject comments anywhere in your code. For example, you see above there is a comment in the middle of a method definition.

Sometimes, new developers feel that if they write comments, it'll mean their code is "less serious". The opposite is true! Well placed comments can mean the difference between code impossible to understand, and code that is understandable.

In your Ruby journey, keep on commenting!

Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.

Top comments (0)