Leave a rating/review
Before moving forward into the next and final part of this course, we’re going to explore another type that will help clarify your code. They are called enumerations. Often times in programming, you will hear the expression, ‘avoid magic numbers’. Take a look at this code. Here we are checking that the current item is greater than eighteen.
But what does the eighteen mean? We have no idea. Is it a unit of time like years or hours. Or maybe its quantity. The eighteen is known as a magic number. A best practice is to use a constant instead.
Often times, we will group constants. For instance, we may have conditions for cold, medium or hot. Or maybe the user selects particular day. For these, we enumerations.
First we define our enumeration by giving it a name. Since this is a type like a class, we uppercase it. Then we put in all the conditions. At which point we run checks on it or even assign to variables.
They are very helpful to make our code self-documenting. So let’s play around with enumerations.
To get started, we’re going to write the damage code for a game. The player can do regular damage, double damage, or quad damage. Open up DartPad and the damage modifiers.
var damageModifiers = [1, 2, 4];
These are the numbers that we’ll use as multipliers against the base damage. Next, lets set the current damage modifier. Set it zero.
var currentDamageModifier = 0;
Next, we’ll set the base damage. Let’s set it to one hundred.
var baseDamage = 100;
Okay, now let’s define the damage types. Let’s create the enum.
enum DamageType { regular, twice, quad }
This is a type like a class so I define it outside the main function. Notice we created a twice damage instead of a double damage. We can use the word double, but remember, double is actually a reserved keyword and while double will still work, it’s a good idea to steer clear of using a keyword as a name to avoid any future issues.
Now let’s set the current damage type. Let’s set it to quad damage.
var currentDamageType = DamageType.twice;
Now we can set the damage modifier based on the type. For this, we’ll use a switch statement. Switch statements work fantastic with enumerations.
switch (currentDamageType) {
case DamageType.regular:
currentDamageModifier = damageModifiers[0];
break;
case DamageType.twice:
currentDamageModifier = damageModifiers[1];
break;
case DamageType.quad:
currentDamageModifier = damageModifiers[2];
break;
}
Now, let’s print out the damage amount.
print('You do ${baseDamage * currentDamageModifier } damage');
Now run the program. And look at that, we get two hundred damage. Now we can actually condense this code. Enumerations keep track of their index. Like arrays, the first enumeration is zero, the second is one and so forth.
Let’s comment out the switch statement.
/**
switch (currentDamageType) {
case DamageType.regular:
damageModifier = damageValues[0];
break;
case DamageType.twice:
damageModifier = damageValues[1];
break;
case DamageType.quad:
damageModifier = damageValues[2];
break;
}
**/
Now lets set the current damage modifier using the index from the enumeration. We do this by accessing the index property.
currentDamageModifier = damageModifiers[currentDamageType.index];
Now run the program. And look at that, we get the same result but the code is cleaner. Nice work.