DEV Community

Discussion on: To comment or not to comment?

Collapse
 
gilblinov profile image
Gil Blinov

Something that wasn't brought up here is writing comments after code has been written and in production.

I'm talking about the need to revisit code due to bugs, refactoring or when the code you're writing now needs to use it.

In these cases, when you don't immediately understand the code and need some mental gymnastics to grok it, instead of writing notes to yourself - do so in comments.

My personal recommendation is to use markdown in a code block to go through the thought process. In some cases I even added flowcharts or sequences using mermaid.js

Usually, this is also a good indication that the code needs to be refactored. Until it happens, your comments will help you or someone else who will need to revisit this code in the future.