Clear Code
  • Introduction
    • About This Book
    • Timeline
    • Software Killers
    • Billion Dollar Mistakes
    • Clear Code Overview
    • Clear Code Rules
  • Data Processing
    • Separate Data Collection And Processing
    • Create Data Models
    • Separate Use Cases
    • Data Should Be Immutable
  • Examples
    • Separate Use Cases With Data Model
  • Maintenance Cost
    • Consider the Maintenance Cost
    • The Software Exists In Time
    • Don't Feed the Monsters
  • OOP
    • Separate Data And Procedures
    • Do Not Use Inheritance
    • When To Avoid Inheritance?
    • What Is The Problem With Abstract Frameworks?
  • VARIOUS
    • The Real Single Responsibility Principle
    • The problem with Clean Code's name
    • How To Handle Warnings
    • Do Not Create Constant Collection Classes
  • Externals
    • Links
    • Quotes
    • Funny
  • Technology
    • Git Tutorials
  • Clean Code
    • Clean Code Introduction
      • Origin & Overview
      • Advanced
      • Typical Issues
    • Clean Code Outline
      • Why Clean Code?
      • Clean Code
      • Clean Code Approaches
      • Specification & Design
      • Duplication
      • Refinement & Refactoring
      • Conventions
      • Names
      • Types
      • Methods
      • Nulls and Validity Checks
      • Comments
      • Dead Code
      • Error Handling
      • Classes
      • Code Formatting
      • Unit Tests
      • Special Cases
      • Object Oriented Programming
      • General Code Smells
    • Clean Code Links
    • Clean Code TOC
    • Effective Java TOC
Powered by GitBook
On this page
  • Write the least possible comments
  • Comments Should Be
  • Bad Comments
  • Good Comments

Was this helpful?

  1. Clean Code
  2. Clean Code Outline

Comments

Write the least possible comments

  • Prefer coding to commenting

  • The code should document itself as most as possible

  • Many and long comments are code smell

Comments Should Be

  • Meaningful

  • Necessary

  • Readable

Bad Comments

  • „Commented out” code - Remove it, find it in the version control system

  • Separators, markers

  • Redundant

  • Misleading

  • Out-of-date

  • Obvious

  • Unnecessary

  • Funny

  • Closing brace comment

  • Attributes - author, date, issue number, etc.

  • Non-English

  • Too many HTML markers

  • Historical

  • Prone to copy-paste error

Good Comments

  • TODO comments

  • Warnings - e.g. "Not threadsafe"

  • Limitations - e.g. "It works only with a sorted list"

  • Javadoc comment to public API - for high level service methods

  • Comment empty blocks

  • Less is more.

Example: Bad: Comments

// Redundant, obvious:
/**
* gets the value of Name
*/
public String getName();

// Funny, unnecessary:
// I invented this part, but do not know why

Example: Good: Javadoc comment

/**
* Returns an Image object that can then be painted on the screen.
* The url argument must specify an absolute {@link URL}. The name
* argument is a specifier that is relative to the url argument.
* <p>
* This method always returns immediately, whether or not the
* image exists. When this applet attempts to draw the image on
* the screen, the data will be loaded. The graphics primitives
* that draw the image will incrementally paint on the screen.
*
* @param url an absolute URL giving the base location of the image
* @param name the location of the image, relative to the url argument
* @return the image at the specified URL
* @see Image
*
* @deprecated Use {@link getImage(Long id)} instead.
*/
@Deprecated
public Image getImage(URL url, String name) {
    try {
        return getImage(new URL(url, name));
    } catch (MalformedURLException e) {
        return null;
    }
}

Last updated 5 years ago

Was this helpful?