In most codebases, there is more legacy code in comments rather than actual comments. While coding we often come across the question of why, where and how to write comments. In most scenarios, we either end up adding unnecessary comments or miss adding any required comments. In this article, we will try and explore the different aspects of comments which will help us in judging & writing productive comments in our code base.
A General disclaimer Comments are the essence of a good program, i.e. it complement the code but does not cover up any bad coding practice. It's the same difference between eating from a food truck & dining in a fancy restaurant. In either of the cases, if the food is not good, it won't sustain. Similarly, if the code is not good enough no comments can be of any help.
Why add comments
Code has two audiences, one is the compiler which interprets it into machine language and the other is humans. Thus, to enhance our readability adding comments is key. It not only helps our peers to understand the code but also helps us to recall any old piece of code authored by us. Within a team, it creates a sense of understanding and encourages effective collaboration. Comments also act as reference notes for the reader to share details or reference links. During PR reviews it becomes easier for the reviewer to understand the underlying logic and resolve conflicts. It also attracts useful suggestions from peers on which to improve. Especially, while working on open-source projects the comments advocate for the code as we directly do not connect with the reviewer. It helps to understand the reviewer from the coder's point of view.Where to add a comment Most of the developers are unaware of where to add a comment. Adding too much of a comment can backfire and diminish the code readability and reusability. The comments require to be added scientifically which can complement the code. Comments in block


No comments:
Post a Comment