Comment Why and not What

Utsavvyas
1 min readMay 30, 2021

While writing any code the most important part is the documentation of the code. Documentation is required for any code so that someone who doesn’t know about the feature or system can easily understand what the system is doing and why some decision has taken while implementing the code. Comments are the most important part of the documentation but people generally write what the code does in the comment which does not add any value.

For example, if you are choosing Threadpool size 10 and you add a comment that thread pool size is 10 will not add any value. We should mention that our thread pool is doing x operation which will require y CPU operation and we have z type of host so thread pool size 10 is good enough. If you want to update the host type to a bigger or smaller host you may consider updating this value.

In the above example, we are explaining why we have chosen the specific number and not what our code is doing. What part of the code should be self-explanatory through the code itself but why we are choosing any design no one can know by reading the code. Comment should explain why so that new people can easily understand the constraints of the system.

--

--