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.
Visualize Dart variables as small containers packed with data. The brief example below highlights how a variable is created in Dart:
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:
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.
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.
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.
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.
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!
