Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Comments and Self-Documenting Code | Naming and Structure
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Clean Code Concepts in Java

bookComments and Self-Documenting Code

The Role of Comments in Java

Comments in Java let you add notes and explanations inside your code. These notes are ignored by the computer when your program runs, but they help you and others understand what your code does. You can use single-line comments with // or multi-line comments with /* ... */.

When Comments Are Useful

  • Explaining why a complex or unusual piece of code is needed;
  • Describing the purpose of a class, method, or variable when it is not obvious from the name;
  • Marking areas that need further work, such as with a // TODO comment;
  • Providing context for tricky algorithms or business rules.

The Power of Self-Documenting Code

Self-documenting code means writing code that is easy to read and understand without needing extra comments. You do this by choosing clear names for your classes, methods, and variables, and by organizing your code into small, focused pieces. When code is self-explanatory, you do not need to add many comments.

Aim to write code that "explains itself" as much as possible. Use comments only when you truly need to clarify something that cannot be made obvious through good naming and structure.

Best Practices for Comments and Self-Documenting Code

Writing clean Java code means making your code easy to read and understand without relying heavily on comments. Follow these best practices to keep your code self-explanatory and use comments effectively:

Use Comments to Explain "Why," Not "What"

  • Focus comments on the reasoning behind complex decisions;
  • Avoid stating what the code does if it is already clear from the code itself;
  • Use comments to clarify intent, business logic, or unusual workarounds;
  • Remove outdated or misleading comments to prevent confusion.

Example:

// Good comment: explains why
// Using a HashMap to improve lookup speed for large data sets
Map<String, User> userMap = new HashMap<>();

// Bad comment: states the obvious
// Create a new HashMap
Map<String, User> userMap = new HashMap<>();

Write Self-Documenting Code

  • Choose clear, descriptive names for variables, methods, and classes;
  • Break down complex logic into smaller, well-named methods;
  • Use consistent formatting and indentation to improve readability;
  • Keep functions short and focused on a single task.

Example:

// Easy to understand without extra comments
public boolean isAdult(int age) {
    return age >= 18;
}

When to Use Comments

  • To explain "why" a non-obvious solution was chosen;
  • To provide context for external dependencies or limitations;
  • To clarify intent when code cannot be made clearer through naming or structure.

Rely on clear code and reserve comments for explanations that code alone cannot express. This approach leads to code that is easier to maintain, review, and extend.

question mark

Which approach best supports writing self-documenting code in Java?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookComments and Self-Documenting Code

Desliza para mostrar el menú

The Role of Comments in Java

Comments in Java let you add notes and explanations inside your code. These notes are ignored by the computer when your program runs, but they help you and others understand what your code does. You can use single-line comments with // or multi-line comments with /* ... */.

When Comments Are Useful

  • Explaining why a complex or unusual piece of code is needed;
  • Describing the purpose of a class, method, or variable when it is not obvious from the name;
  • Marking areas that need further work, such as with a // TODO comment;
  • Providing context for tricky algorithms or business rules.

The Power of Self-Documenting Code

Self-documenting code means writing code that is easy to read and understand without needing extra comments. You do this by choosing clear names for your classes, methods, and variables, and by organizing your code into small, focused pieces. When code is self-explanatory, you do not need to add many comments.

Aim to write code that "explains itself" as much as possible. Use comments only when you truly need to clarify something that cannot be made obvious through good naming and structure.

Best Practices for Comments and Self-Documenting Code

Writing clean Java code means making your code easy to read and understand without relying heavily on comments. Follow these best practices to keep your code self-explanatory and use comments effectively:

Use Comments to Explain "Why," Not "What"

  • Focus comments on the reasoning behind complex decisions;
  • Avoid stating what the code does if it is already clear from the code itself;
  • Use comments to clarify intent, business logic, or unusual workarounds;
  • Remove outdated or misleading comments to prevent confusion.

Example:

// Good comment: explains why
// Using a HashMap to improve lookup speed for large data sets
Map<String, User> userMap = new HashMap<>();

// Bad comment: states the obvious
// Create a new HashMap
Map<String, User> userMap = new HashMap<>();

Write Self-Documenting Code

  • Choose clear, descriptive names for variables, methods, and classes;
  • Break down complex logic into smaller, well-named methods;
  • Use consistent formatting and indentation to improve readability;
  • Keep functions short and focused on a single task.

Example:

// Easy to understand without extra comments
public boolean isAdult(int age) {
    return age >= 18;
}

When to Use Comments

  • To explain "why" a non-obvious solution was chosen;
  • To provide context for external dependencies or limitations;
  • To clarify intent when code cannot be made clearer through naming or structure.

Rely on clear code and reserve comments for explanations that code alone cannot express. This approach leads to code that is easier to maintain, review, and extend.

question mark

Which approach best supports writing self-documenting code in Java?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
some-alt