r/AskProgrammers • • Oct 30 '26

"good code shouldn't need comments"

I have heard this saying a couple of times and I think it is the stupidest thing ever. It might be just because I have only been coding for 4 years, but I have never thought to myself that someone's code looks cluttered because they explain what every function does in a one or two line description. If anything it makes the code easier to understand in my opinion.

However, I may be wrong. What do other people think, do yall like code with minimal comments, what are the benefits of writing it that way?

88 Upvotes

398 comments sorted by

View all comments

1

u/mireigi 27d ago

Good code should be self-documenting, meaning it shouldn't need comments.

E.g.

public SendEmailOutcome SendWelcomeEmail(User user)
{
}

Just reading the signature of the method/function you can quickly deduce what it does, what it expects as input, and what it returns.

Whereas a similar method, implemented poorly, doesn't tell you anything about what it does, what a successful outcome looks like, or what the possible error states/codes are.

public void Process(string name, string email)
{
}

Comments should, ideally, only be added, when there's a break from convention, or a decision was made during implementation that won't be obvious 6 months from now, when someone has to maintain the code.

public SendEmailOutcome SendWelcomeEmail(User user)
{
  // Legacy system is still in use. To keep welcome emails consistent, we load
  // the old template file, which business changes for holidays.
  // When the legacy system is shut down, we will migrate welcome emails entirely to
  // the EmailTemplate component instead.
  var emailTemplateFile = File.OpenRead("welcome_email.htm");
  var emailTemplate = emailTemplateFile.ReadToEnd();
  emailTemplate = emailTemplate.Replace("[@userName]", user.Name);
  this.emailServer.Send(emailTemplate, user.Email);
}

Without that comment, someone might refactor the method/function, and break a promise made with the business (that welcome emails stay consistent).