Comments

In programming, comments are lines of text that are written in the code to provide additional information about the code to other developers or to the future self.

Comments are not executed by the computer, they are simply ignored by the interpreter or compiler.

In JavaScript, there are two ways to write comments:

  1. Single-line comments: Single-line comments start with two forward slashes (//) and continue until the end of the line. Anything written after the double slashes is considered a comment.

Example:

Loading Code . . .
  1. Multi-line comments: Multi-line comments start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). Anything written between these symbols is considered a comment.
Loading Code . . .

Comments are useful in programming because they allow you to add notes to your code that explain what the code does, why it does it, or how it does it. Comments also make it easier for other developers to read and understand your code. It is always a good practice to add comments to your code as it helps in making the code more readable, maintainable, and easier to understand.

Cautions

  1. If you think that your code needs a lot of comments, then it is a sign that your code is not readable enough. With experience and learning good practices, you will be able to write code that is easy to understand without the need for comments.
  2. Very long comments can make your code hard to read. Try to keep your comments short and to the point. If someones needs to change your code, probably they won't update the comments as well. So, it is better to write code that is easy to understand without the need for comments.
  3. If you need to avoid a piece of code anymore, you can simply delete it. There is no need to comment it out. If you need to keep it for some reason, you can use a version control system like Git to keep track of the changes in your code.

Dos:

  • Do write comments that explain what the code does, why it does it, or how it does it.
  • Do use comments to document complex code, edge cases, or any other details that might not be obvious at first glance.
  • Do write comments that are clear, concise, and easy to read.
  • Do use consistent formatting for your comments, such as starting with a capital letter and ending with a period.
  • Do update your comments when you modify your code, so that they remain accurate and useful.

Don'ts:

  • Don't write comments that simply repeat the code or describe what is already obvious.
  • Don't write comments that are rude or unprofessional.
  • Don't write comments that contain sensitive or confidential information.
  • Don't use comments as a substitute for clean and well-organized code.

Good practices:

  • Use comments to explain the purpose of your functions, variables, and classes.
  • Use comments to document the parameters of your functions and their expected behavior.
  • Use comments to describe any assumptions or constraints that your code depends on.
  • Use comments to provide links to external documentation or resources.
  • Use comments to explain any workarounds or temporary fixes that you have implemented.
  • Use a consistent style for your comments, such as a standard format or syntax.

Exercise

Drag and drop the words to complete the sentence correctly.

© 2024 - ®Mewters