Dart Comments

Document your code properly with comments

๐Ÿ’ฌ Dart Comments

Comments are text notes in your code that explain what the code does. They are ignored by the Dart compiler and help make your code more readable and maintainable for yourself and other developers.


// This is a single-line comment
void main() {
  print('Comments help explain code!'); // Another comment
}
                                    

Output:

Comments help explain code!

Types of Comments

//

Single-line

Comments for one line of text

// This is a comment
/* */

Multi-line

Comments spanning multiple lines

/* This is a
   multi-line comment */
///

Documentation

Special comments for documentation

/// This documents a function
๐Ÿšซ

Code Disabling

Temporarily disable code

// print('This won't run');

๐Ÿ”น Single-line Comments

Use // to create single-line comments:

void main() {
  // This is a single-line comment
  print('Hello World');
  
  // You can add comments above code
  var name = 'John'; // Or at the end of lines
  
  // Comments can explain what the code does
  var age = 25; // Store the user's age
  
  print('Name: $name, Age: $age');
}

Output:

Hello World

Name: John, Age: 25

๐Ÿ”น Multi-line Comments

Use /* */ to create comments that span multiple lines:

void main() {
  /*
   * This is a multi-line comment
   * It can span several lines
   * Useful for longer explanations
   */
  
  print('Multi-line comments are useful');
  
  /* 
  You can also write them like this
  without the asterisks
  */
  
  var result = 10 * 5; /* Inline multi-line comment */
  print('Result: $result');
}

Output:

Multi-line comments are useful

Result: 50

๐Ÿ”น Documentation Comments

Use /// to create documentation comments for functions and classes:

/// This function calculates the area of a rectangle
/// [width] - The width of the rectangle
/// [height] - The height of the rectangle
/// Returns the area as a double
double calculateArea(double width, double height) {
  return width * height;
}

void main() {
  /// Calculate area for a room
  var roomArea = calculateArea(10.0, 12.0);
  print('Room area: $roomArea square meters');
}

Output:

Room area: 120.0 square meters

๐Ÿ”น Commenting Best Practices

Follow these guidelines for effective commenting:

โœ… Good Comments:

  • Explain WHY , not what the code does
  • Describe complex logic or algorithms
  • Add context for business rules
  • Document function parameters and return values

โŒ Avoid:

  • Obvious comments that repeat the code
  • Outdated comments that don't match the code
  • Too many comments that clutter the code
  • Comments that explain bad code instead of fixing it
void main() {
  // Good: Explains the business rule
  var discountRate = 0.1; // 10% discount for new customers
  
  // Bad: States the obvious
  // var age = 25; // Set age to 25
  
  // Good: Explains complex calculation
  var tax = price * 0.08; // State sales tax rate
  
  // Good: Explains why we do something
  if (age < 18) {
    // Minors need parental consent for account creation
    requireParentalConsent();
  }
}

๐Ÿ”น Commenting Out Code

Use comments to temporarily disable code during development:

void main() {
  print('This code runs');
  
  // Temporarily disabled for testing
  // print('This code is commented out');
  // var unused = 'This variable is disabled';
  
  /*
  You can also comment out multiple lines
  print('Line 1');
  print('Line 2');
  print('Line 3');
  */
  
  print('This code runs too');
}

Output:

This code runs

This code runs too

๐Ÿ”น Practical Example

A well-commented Dart program:

/// Simple calculator program
/// Demonstrates proper commenting techniques
void main() {
  // Input values for calculation
  var num1 = 15;
  var num2 = 4;
  
  /* 
   * Perform basic arithmetic operations
   * and display the results
   */
  
  // Addition
  var sum = num1 + num2;
  print('$num1 + $num2 = $sum');
  
  // Division with decimal result
  var division = num1 / num2; // Use / for decimal division
  print('$num1 รท $num2 = $division');
  
  // TODO: Add more operations later
  // print('More operations coming soon!');
}

Output:

15 + 4 = 19

15 รท 4 = 3.75

๐Ÿง  Test Your Knowledge

Which symbol is used to start a single-line comment in Dart?