Say No to Boilerplate Comments
In general, I do not believe in comments in code as they:
- add programmer effort to interpret.
- can become outdated quickly.
- indicate a bigger problem with the programmer or implementation.
Whenever I come across a production Ruby on Rails app and see the
original boilerplate comments such as in:
config/initializers/application_controller_renderer.rb
I really have to wonder: what is so important about Ruby on Rails’ default comments that they have to exist… even when they are essentially… useless!
-
When upgrading the projects’ Rails version, are comments also upgraded to the latest version?? Think about this.
-
Are the default comments so important that they must exist in the project and source code repository the whole time or things will fail? Remember, this is a code comment.
-
Do the comments only ever exist once and if they are not saved, they disappear forever? Like photos on a certain ephmeral social service? No, just run
$ rails new default_comments
and tada, you have the original comments! -
I also have to argue, the comment was not written by anyone on the current team. If you have questions on how to interpret the comment, how do you do that?
So these comments’ existence on production applications perplexes me. Why are the original comments in a Ruby on Rails project exist after the projects’ deployed to production?
There’s probably any number of reasons: we wanted to move fast, we didn’t know the app would take off, the comments are everywhere, it’s already working in production.
Solution
Whatever the reason, there’s a solution: the below script on github removes all the comments from a Ruby on Rails 5.2.0 project:
I haven’t tested it out yet, but it’s an automated solution to removing boilerplate comments!
Conclusion
Honestly, just delete unnecessary comments, especially boilerplate comments generated by the framework. Boilerplate comments are generally bad as they:
- are never upgraded for the app, even when upgrading the underlying framework.
- do not contribute to anything in the actual code base.
- easily generated again by using the framework to create a new project.
- were never written by anyone on the team!
If you want to remove Ruby on Rails’ boilerplate comment from your production application in an automated fashion, try Matias’ skip_rails_comments!.
Thanks Matias!