Introduction
Comments - it would seem that it could be simpler, and why write a whole article. But it's not that simple. As my boss said, anyone can write code, but writing a good comment is difficult. Most language courses start with the traditional Hello World. Even in Oracle Tutorials, in the "Getting Started" section, we start with The "Hello World!" Application . And from the very first lines of code we see them - Java comments. Their importance is also emphasized by the fact that in such an important document as the Java Code Convention, comments are given a separate section: Comments . According to the documentation, comments in Java are divided into two types:- implementation comment (or code comment);
- documenting comment.
Java code comments
From the name it is clear that this comment relates to the code and should reflect its features. Code comments are:-
Lowercase (i.e. described in one line)
// Строчный комментарий System.out.println("Hello, World!");
-
Block (i.e. they are described as a whole block, because they do not fit on one line)
/* * Блочный комментарий */ System.out.println("Hello");
Every time you write a comment, wince and feel like a failure."It is clear that there is no absolute truth, and sometimes comments are necessary. But there are always options, and unnecessary comments need to be fought. This chapter also mentions unusual comments, TODO:
// TODO: Добавить World
System.out.println("Hello, ");
The point of them is that they can be handled in a special way in the IDE. For example, in IDEA they are collected on a separate tab, where you can view them:
Comments for documentation
Documentation comments describe the public API. API is the application programming interface, that is, those classes and methods that are available to other developers to perform any actions. In short, these comments should explain why this or that class and package was created and what this or that method does. You can also describe class fields if necessary. This is exactly what we see in the tooltips of our IDEs, which is presented as a JavaDoc. For example:package test;
/**
* This is a JavaDoc class comment
*/
public class JavaDocTest {
/**
* This is a JavaDoc public field comment
*/
public static final String HELLO_MESSAGE = "Hello, World!";
public static void main(String... args) {
JavaDocTest.greetings();
}
/**
* This is a JavaDoc public method comment
*/
public static void greetings() {
System.out.println(HELLO_MESSAGE);
}
}
After this, we can run the following command from the directory that contains our package directory: javadoc -d ./test test
After this, we will see the documentation generation process.
Conclusion
As we can see, such a seemingly simple thing as comments turns out to be much more complicated in reality. Therefore, if you spend some time on comments and follow them, your code will be better and you will be more valuable as a programmer. #ViacheslavWhat else to read: |
---|
GO TO FULL VERSION