How to make variables with Dart conventions (Class 9)

Variables with Dart Conventions

Today, we are going to understand the process of making variables. Let’s start.

 

Statically/ explicitly typed variables

void main() {
  String name = “Ali”;
  int rollNo = 45;
  double gpa = 3.55;
  bool isChecked = false;
}

In the above code, to understand suppose that on the left side, we have created a box and on the right side we assigned values. Whereas = is an assignment operator. It means, in the above coding, String/int/double/bool are variable types and name/rollNo/gpa/isChecked are names of variables with = assign operator. Moreover Ali, 45, 3.55, false are values. As we have mentioned value with Variable type is, therefore, considered as Statically/ explicitly typed variables. It is to be noted that we cannot change values and we have to use the same variable as per variable type.

 

Inferred variables

In this case, we don’t need to mention type and we will just use var keyword. It is to be noted that type is mentioned on runtime as it would be converted as per our value. It is also evident that inferred variables can accept any type of variable without any issue. For example, if we take the previous example then it would be as under:-

void main() {
  var name = “Ali”;
  var rollNo = 45;
  var gpa = 3.55;
  var isChecked = false;
}

 

Now in the above coding, we can change the value whenever required. This means we can change “Ali” to 45 etc.

 

Dart recommendations:-

Dart recommends using statically/ explicitly typed variables. In this type of variable, errors are immediately highlighted but we can use inferred variables as well depending upon our needs.

 

How to name a variable:-

We need to name a variable/ entity to identify it.  There are specific naming rules, conventions, standards, and protocols. It is noted that the first impression of the developer is judged by watching variable naming or used conventions. Though we can complete our work without these conventions too, however, it is not deemed as a good impression or approach.

 

Recommendations for naming a variable

The recommendations for naming a variable or rule are as under:-

* Entity names can only have alphabets, numbers, underscore, and dollar signs.

* Entity name can’t start with a number

* Entity name can’t contain white spaces

* Entity name can’t be exactly the same as a keyword for example var

* Entity can’t be duplicated

* Entity can’t contain special characters except _ $

* Entity should be descriptive and short

 

Cases/ conventions

There are two main conventions/ cases in Dart:

  1. Camel Case
  2. Snake Case

 

Camel Case:

In camel case, there are two types i.e. Lower Camel Case & Upper Camel Case.

 

Lower Camel Case:

In this scenario, we type the first complete word in small letters and then every first letter of the next word in capital letters like lowerCamelCase. More examples are myName, studentRollNo, etc. The variables, constants, functions, list, set, map, hash map, class properties, and functions should be in Lower Camel Case.

 

Upper Camel Case:

In this scenario, we type every first letter of the word in a capital letter like UpperCamelCase. Classes, Interfaces, and Enum Class should be in the Upper Camel Case.

 

Snake case

In this scenario, we type _ after every word in small letters like snake_case. The project name, project file name, folder name, key name, and API keys should be in snake_case. For example best_cricket_team.

 

Variable declaration/ creation

In some cases, we just create variables without giving them value. We can give them value later on. For example

void main() {
  String name;
  int rollNo;
  double gpa;
  bool isChecked;
}

 

Making multiple variables in a single line

We can also make multiple variables in a single line like:

String name, fatherName, motherName, studentName;
int id, studentId, userId, adminId;
double gpa, studentGpa, cGpa;
bool isCheck, isMarried, fileCheck;

 

Initialization in a single-line

Similarly, we can also declare or do initialization in a single line like:

String name =”Ali”, fatherName = “Ahsan”, motherName =”Aisha”, studentName = “Umer”;

 

 

Updation of Variables

It is also to note that the values of variables can also be updated and it will be considered final as under:-

fatherName = “Ashraf”;

 

If we know the above basics of Dart then we can easily proceed with Mobile App Development !!

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

 

 

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.