In clean code, although we have meaningful names, are there cases that we still need comments?



Yes, even in clean code where meaningful names are used, there are still cases where comments are necessary and beneficial. Here are a few scenarios where comments are important:

Complex Logic: If the code involves complex algorithms or logic that is not immediately obvious, comments can provide clarity. This helps other developers understand the rationale behind certain decisions or the approach taken.

Workarounds and Temporary Fixes: Sometimes code may include workarounds or temporary fixes for bugs or issues. Comments in such cases are crucial to explain why this non-standard approach was necessary and under what conditions it can or should be changed or removed.

References to External Resources: If the code is based on or related to external documentation, standards, or resources (like a specific RFC or a research paper), including a comment with the reference can be very helpful for future understanding and maintenance.

Intent and Context: While good naming conveys what the code is doing, it may not always convey why it is doing it. Comments can be used to describe the intent behind a block of code, especially when the 'why' is not immediately obvious from the code itself.

TODOs and FIXMEs: Comments are often used to mark areas of the code that need future attention, like optimizations, enhancements, or areas where a more robust solution is needed.

Non-Obvious Consequences or Implications: Sometimes, a piece of code might have side effects or implications that are not immediately apparent. A comment can be helpful to warn future maintainers of these consequences.

Code Generation: In cases where code is automatically generated, comments can be used to indicate that the code should not be modified manually, or to explain how to update the generated code.

Remember, the key to effective commenting is to provide useful information that is not readily apparent from the code itself. Over-commenting, or stating the obvious, can clutter the code and reduce readability. The goal should always be to write clear, self-explanatory code as much as possible, using comments judiciously to enhance understanding where necessary.

Hiç yorum yok

Rastgele İçerik

DonanımHaber

© tüm hakları saklıdır
made with by templateszoo