This is in continuation to the Quick Tips for writing Clean Code Part-1 and in this blog, we will be discussing the below points:
Comments
- Redundant Comment
- Intent Comment
- Apology Comment
- Warning Comment
- Zombie Code
- Divider Comment
So, the first thing to keep in mind while writing comments is to use comments only when your code alone isn't sufficient.
We should always prefer code over comments and write code in a way which is self-expressive and clear in intent.
Redundant Comment
- public class User {
- private DateTime _dateOfBirth;
- //Constructor
- public User(DateTime dateOfBirth) {
- _dateOfBirth = dateOfBirth;
- }
- //This method is used to calculate age
- public int CalculateAge() {
- return DateTime.Now.Year - _dateOfBirth.Year;
- }
- //This method is used to get the date of birth
- public string GetDOB(string formatter) {
- return _dateOfBirth.ToString(formatter);
- }
- }
The problem with repeated comments is that they violate the DRY principle and really add no value.
So, it’s not necessary to add a comment for a method if your method is named well.
- void Main() {
- var counter = 1; // Set the counter to 1
- var user = new User(new DateTime(1988, 10, 9));
- user.CalculateAge().Dump();
- user.GetDOB("dddd, dd MMMM yyyy HH:mm:ss").Dump();
- user.GetDOB("dd/MM/yyyy").Dump();
- }
OUTPUT

Intent Comment
Rather than mentioning intent using comments try to clarify the intent of the code in the code itself like using
- Improved function names.
- Constants or enum.
- Intermediate variables etc.





Join the conversation! Your thoughts help the community grow.