Free Ebook cover Learn to program in complete Java, from programming logic to advanced

Learn to program in complete Java, from programming logic to advanced

3.5

(4)

238 pages

Java Best Practices and Coding Standards: Comments and Documentation

Capítulo 160

Estimated reading time: 5 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

  • 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.

The correct statement about Java best practices is Option 3. Following a consistent set of coding standards, such as using meaningful variable names and maintaining consistent indentation, is crucial for code quality and readability. The text emphasizes the importance of coding standards, including naming conventions and code structure, to ensure maintainability and understanding among developers.

Next chapter

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

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.