How often should I leave comments within my code?

Question

How often should I leave comments within my code?

Answer

A good rule of thumb here is to leave comments for any piece of code that might be non-obvious to yourself or anyone who might be reading your code.

If you are working on a team, you want to ensure that your team members can understand your code. If you are working on an open source project, you want to ensure the wider development community can understand your code. If you are working on a solo project, you want to ensure that your future self can understand your code.

As far as workflow is concerned, it is not uncommon to leave comments both as you code and after you have written some code.

41 Likes

Can I make comments after the </html>? Will the browser ignore what I wrote after </html>?

9 Likes

A very late reply, but it bears mention that anything outside of <html></html> will not be regarded as HTML, so the comment (with tags) MAY render to the display, which is not something we would want. Nothing should ever be written outside of the root element.

62 Likes

Can I comment in the element section?
Or just the body section?

2 Likes

You can comment anywhere except in strings and before and after <html></html>

20 Likes

3 posts were split to a new topic: Is it important to comment our code?