45.4 Java Best Practices and Coding Standards: Comments and Documentation

Programming in Java, or any other programming language, is not just a matter of making the code work. It is essential that the code is readable, maintainable and understandable not only for those who write it, but also for other developers who may work with it in the future. In this context, comments and documentation play a crucial role.

Comments in the Code

Comments are snippets of text within the source code that are ignored by the compiler and serve to explain what the code is doing, why certain decisions were made, or to provide any other relevant information that is not immediately obvious from of the code itself.

In Java, comments can be single-line, starting with //, or multi-line, starting with /* and ending with */ . Additionally, there is the documentation comment, which starts with /** and ends with */, which is used to generate automatic documentation with the Javadoc tool.

Good practices regarding comments involve:

  • Avoid Obvious Comments: Do not comment on what is evident from the code. Comments should be used to explain why, not what.
  • Keep Updated: An outdated comment is worse than no comment. Always update comments when modifying code.
  • Avoid Unnecessary Comment Blocks: Instead of using a comment block to disable code, prefer to use a version control system to maintain the history of changes.
  • Write Meaningful Comments: Comments should add value to the code by explaining complex or non-obvious decisions, assumptions, dependencies, and algorithms.

Documentation with Javadoc

Javadoc is a tool for automatically generating API documentation in HTML format from comments in Java source code. Documentation comments begin with /** and are placed before definitions of classes, interfaces, constructors, methods, and fields.

A good practice is to publicly document all APIs, as this helps other developers understand how to use your classes and methods without having to dive into the source code. Documentation must include:

  • Description: A clear explanation of what the method or class does.
  • Parameters: Each parameter must be documented with the @param tag, explaining its purpose.
  • Return Value: If the method returns a value, it must be explained with the @return tag.
  • Exceptions: All exceptions that the method can throw must be documented with the @throws or @exception tag.
  • Usage Examples: In some cases, examples of how to use a method or class can be extremely useful.

In addition, Javadoc documentation supports HTML tags, which allows for richer formatting and the inclusion of lists, tables and links.

Coding Standards

Following a consistent set of coding standards is essential to maintaining code quality. Coding standards range from the naming of variables, classes, and methods to the structuring of code blocks and design patterns.

Some general rules include:

  • Nomenclature: Use meaningful and self-explanatory names. Variables must be in camelCase, classes in PascalCase, and constants in UPPER_SNAKE_CASE.
  • Indentation: Be consistent with indentation, using spaces or tabs (usually 4 spaces).
  • Braces: Follow a consistent style for using braces. The most common style in Java is "K&R", where the opening brace is on the same line as the code that precedes it.
  • Blank Lines: Use blank lines to separate related blocks of code within a method.
  • Limit Line Length: Avoid very long lines, as they make the code difficult to read. A common limit is 80 or 120 characters.

Additionally, design patterns are generalized solutions to common problems in software design. Knowing them and applying them when appropriate can significantly improve the quality of the software project.

Conclusion

Comments and documentation are essential aspects of writing clean, maintainable code.elevel in Java. They help other developers understand the code faster and make long-term maintenance easier. Following coding standards and using design patterns are best practices that contribute to code quality and consistency. By taking the time to properly comment and document code, developers not only improve their own efficiency, but also that of their teams and the developer community as a whole.

Now answer the exercise about the content:

Which of the following statements about best practices in Java, as described in the text, is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Java best practices and coding standards: Using Javadoc comments to document the API

Next page of the Free Ebook:

161Java best practices and coding standards: Using Javadoc comments to document the API

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text