Dart – 8 – Lexical Structure (identifiers, keywords, comments)

Lexical Structure in Dart Programming

The lexical structure of a programming language defines the basic building blocks of code, including how you name variables, specify keywords, and add comments. In Dart, a modern and expressive language, a well-defined lexical structure is crucial for writing clean and readable code. In this discussion, we’ll explore key aspects of Dart’s lexical structure, including identifierskeywords, and comments.

Identifiers in Dart

Identifiers are names given to various elements in your Dart code, such as variables, functions, classes, and more. Dart imposes certain rules and conventions for creating valid identifiers:

  • Identifiers must start with a letter or an underscore (_) character.
  • They can be followed by letters, digits, or underscores.
  • Identifiers are case-sensitive, meaning myVariable and myvariable are treated as distinct identifiers.
  • They cannot be Dart keywords (reserved words used for language features).

Here’s an example of valid identifiers in Dart:


int _privateVariable = 42;
String userName = 'Alice';
    
Dart Keywords

Keywords are reserved words in Dart that have predefined meanings and are used to define language features. You cannot use keywords as identifiers in your code. Some common Dart keywords include ifelsewhileclass, and many more.

It’s important to avoid naming your variables, functions, or classes with keywords, as it can lead to syntax errors and confusion in your code. Here’s an example of using a Dart keyword as an identifier:


String if = 'conditional'; // Error: 'if' is a keyword and cannot be used as an identifier.
    
Comments in Dart

Comments are essential for documenting your code, making it more understandable and maintainable. Dart supports two types of comments: single-line comments and multi-line comments.

Single-line comments begin with two forward slashes (//) and continue until the end of the line. They are often used for brief explanations or notes within the code.


// This is a single-line comment.
int x = 10; // Assigning a value to x.
    

Multi-line comments start with /* and end with */. They can span multiple lines and are suitable for more extensive documentation or for temporarily disabling blocks of code.


/*
This is a multi-line comment.
It can span multiple lines for detailed explanations.
*/
int y = 20;
/* Commented out code:
int z = 30;
*/
    
Documentation Comments

In addition to regular comments, Dart allows you to write documentation comments to generate documentation for your code. These comments begin with /// for single-line documentation or /** ... */ for multi-line documentation.


/// This is a single-line documentation comment.
String name = 'Bob';

/**
 * This is a multi-line documentation comment.
 * It can provide detailed documentation for functions, classes, or libraries.
 */
void greet(String name) {
    print('Hello, $name!');
}
    

Documentation comments are a powerful way to create API documentation for your code that can be automatically generated into readable documentation for others to use and understand.

Conclusion

Understanding the lexical structure of Dart, including identifiers, keywords, and comments, is essential for writing clean and comprehensible code. Valid identifiers ensure your code is well-named and follows Dart’s conventions, while comments and documentation comments help in documenting your code for yourself and others. By following these principles and practices, you can write more maintainable, readable, and effective Dart programs.