What are comments?
Sometimes, when writing 🖊️ the code, you will need to provide explanations that are intended only for humans а and not for the computer. Perhaps it will be some kind of message to yourself from the past (to the Future Me!) About this or that thing in the program code. Or maybe it will just be an indication that you need to run only such and such a part of the code, and the rest should be omitted. In short, in any such case, you will need to resort to the help of comments. Comments 🗣️ are used to add ➕ hints, notes 🔖, suggestions or warnings. They can make the code easier to read and understand. They can also be used to prevent 🛑 code execution (this is a common practice when debugging code).
Single line comment
The first 1️⃣ way is
// comment 🗣️ in this case all text 📜 in the line after the double slash will be a comment 🗣️. When the interpreter sees two 2️⃣ slashes in a row
//, it immediately understands that everything that goes further to the end of the line can be safely ignored. For now, ignore the syntax 📖 of the code, we will get to know it in detail a little later.
The second 2️⃣ way is
/ * * /, which is more flexible than the first 1️⃣. As soon as the interpreter sees
/ *, it thinks: "Yeah, so until I see
* /, I can not pay any attention to anything here!"
For example, you can use it in one 1️⃣ line 👇:
Alternatively, you can make a multi-line comment 🗣️ as shown here 👇:
Comment in the middle of the code
Also, if you wish, you can use this commenting 🗣️ in the middle of a line of code. Although this may impair the readability of the code👇:
However, there are ways to reduce the need for comments. We can use certain coding techniques to refine our code simply by taking advantage of the programming language to our advantage.
This not only makes our code easier to understand, but also helps to improve the overall design of the program!
This type of code is often referred to as self-documenting.
This means that the code must be written so that, firstly, in the future, it would be convenient for other programmers to work with it, including its author, and secondly, taking into account the possibility of extending this code.
With the commenting on the code, that's all and the call for a break sounded.
Write to Discord chat.
Who are the comments for?
What are comments not used for?
- Adding tips, notes, suggestions
- As part of the program code
- To prevent code execution
/ * * /
How do I disable any number of lines of code?
- Write a comment in the middle of the code
- Wrap the code in a comment
- Use the command
What term refers to code that has useful variable names and descriptive function names so that the developer can easily understand the purpose of the code as he reads it (without much need for explanatory comments)?
In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.
- MDN web docs
Thanks goes to these wonderful people (emoji key):
🐛 🎨 🖋