Syntax and Structure in Dart

In every programming language, there are some rules and regulations that we need to follow to write a program. These rules and regulations are called syntax.

Dart is a case-sensitive language. It means uppercase and lowercase letters are treated differently. For example, the variable name and Name are two different variables in Dart.

You are already familiar with this topic? Try playing quiz

Example

Let’s see a simple example of Dart program. Every dart program starts with the main() function. The main() function is the entry point of the program.

  void main() {
  print('Hello, World!');
}
  

Output

  Hello, World!
  

code Try Yourself

Dart Naming Conventions and Styles

Any stupid programmer can write a bunch of codes but the good one writes the high quality and testable code. Let’s understand what makes a dart code high quality.

Identifers

There are three types of identifers in dart. They are :

  • Upper Camel Case
  • Lower Camel Case
  • Lower camel case with underscores (_)

UpperCamelCase Identifier in Dart

Did you notice the capital letter at starting of every words? That’s what is called upper camel case. UpperCamelCase capilitizes the first letter of each word including the first word. This kind of identifers should be used while naming any class, enums, typedefs and extensions.

Example:

  class YourClassName{ ... }

enum YourEnumShouldBeNamedLikeThis{...} 

typedef FunctionReturnType = bool function();  

extension MyStringExtension on String{...}
  

lowerCamelCase Identifer in Dart

lowerCamelCase capalitize the first letter of eachword starting from the second word. This kind of identifers should be used while naming varibales, parameters and constants.

  
int noOfBoys = 34; 

const noOfDaysInWeek= 7; 

void calculateTax({required double basicSalary}) {...}
  

lower_camel_case with underscore (_) in Dart

lower_camel_case with underscore in dart has all the letters of the world in small letters each seperated by underscore (_). This kind of idenfiers are best used for naming folders and files in dart.

  - folder_name
    - file_name.dart
    - file_name_one.dart
    - file_name_two.dart
  

Quiz

Participate in the quiz to check your knowledge.