Lesson 2

Understanding Variables and Constants in Dart

Topic Overview and Goal Setting

Welcome back! As we continue our Dart Decoding Adventure, we're preparing to gain a better understanding of Dart Variables, our cornerstone concepts. Like bricks in a building, variables form the base of our code, enriching it with data and authenticity.

Essentially, a variable in coding is much like a container — it's a specific spot in memory where a value can be held. This lesson aims to demystify the concept of Dart variables, delving into their definition, the naming conventions, value assignments, and thoughts on immutable variables.

What are Dart Variables?

Visualize Dart variables as small containers packed with data. The brief example below highlights how a variable is created in Dart:

Dart
1int numberOfPlanets; // We declare a variable, akin to choosing a container 2numberOfPlanets = 8; // We then fill it with a value 3print(numberOfPlanets); // Finally, we validate its contents. It outputs: 8

Here, int depicts the variable's data type (integer number), numberOfPlanets is the variable's identifier, and 8 is its value. We'll touch upon data types in our next lesson, so relax if the int part isn't clear as of now.

However, you can also declare and assign a value to a variable in a more succinct manner. Here's an example:

Dart
1int numberOfPlanets = 8; // Declaring and assigning the variable with a value at the same time 2print(numberOfPlanets);
Dart Naming Conventions

Naming a Dart variable requires adherence to certain rules and conventions, much like correctly labeling a container. These ensure that your code avoids errors and remains interpretable to others.

The variable name should begin with a lowercase alphabet following the CamelCase notation: if the variable name has just one word, it should be entirely lowercase; for variable names with multiple words though, the first one should be lowercase, and the remaining ones should begin with a capital letter. Examples include age, weight, myAge, firstDayOfWeek.

Character such as digits and special symbols cannot be the leaders in variable names.

Dart
1// Appropriate variable naming 2int myAge = 24; 3int myWeight = 65; 4int area51Population = 0; 5 6// Incorrect variable naming (commented intentionally) 7// int 0zero = 0; 8// int ~tildeMark = 1;
Assignment Operations in Dart

Assigning in Dart involves transmitting or updating a value to a variable using the = operator. This mechanism is akin to depositing an item in a container.

Dart
1int galaxies = 54; // We devise a container and label it before putting a value inside 2print(galaxies); // We examine its contents. It outputs: 54 3 4galaxies = 100; // We update the value of the variable 5print(galaxies); // We examine the refreshed contents. It outputs: 100
Dart Constants

While the previous segment illustrates how to modify the variable's value, Dart also allows you to establish constants, that is, variables that are prohibited from changing their value post assignment. To declare a constant, we use the final keyword. The convention for naming constants involves uppercase letters with words separated by an underscore _.

For reasons such as legibility, security (to avoid accidental modifications), and occasionally for performance, it's good practice to mark a value as final if you are confident about its immutability.

Dart
1final int DAYS_IN_YEAR = 365; // We seal a constant, almost like inscribing a fact in stone 2print(DAYS_IN_YEAR); // We scrutinize our etched fact. It outputs: 365 3 4// Attempting to change the value of a final variable 5// DAYS_IN_YEAR = 366; // This would cause a compilation error such as: "Error: Can't assign to the final variable 'DAYS_IN_YEAR'."

Here, DAYS_IN_YEAR acts as a constant, resisting changes post its assignment. Trying to change its value, as shown, results in a compilation error because the value for this variable is immune to changes after it's assigned. This ensures that once a value is set to a final variable, it remains constant throughout the program, enforcing immutability.

Lesson Recap and Next Steps

Congratulations! You've now unlocked insights into the foundational aspects of variables and constants in Dart. In our forthcoming lessons, we'll apply these concepts in real-time coding exercises. Regular practice is crucial to transforming knowledge into abilities, so let's dive into the tasks and maintain our momentum in our Dart Decoding Adventure!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.