Issues with conditionals

The next thing we need to consider is how we should do something based on what state we are in. When writing programs, conditional clauses such as the if and switch statements that we learned about earlier may make your code more difficult to manage. Sometimes, when writing code for specific functionality, writing if statements is completely understandable, especially if it makes sense when you are writing it. For example, the following code makes perfect sense:

void MinValue(int a, int b) 
{
if (a < b)
return a;
else
return b;
}

// Could also be written in the following way:
void MinValue(int a, int b)
{
return (a < b) ? a : b;
}

However, if you are writing something where you are checking what the type of an object is, or whether a variable is of a certain type, that is a bit of an issue. For instance, look at the following function:

void AttackPlayer(Weapon * weapon) 
{
if (weapon.name == "Bow")
{
ShootArrow(weapon);
}
else if (weapon.name == "Sword")
{
MeleeAttack(weapon);
}
else
{
IdleAnimation(weapon);
}
}

As you can see, if we start going down this path, we will need to add many different checks throughout our project, which will make our code hard to change if we ever decide to add more things to support here. First of all, instead of a bunch of if/else statements, when we see something that's comparing the same value and doing something based off of that value, we should be using a switch statement, like we did earlier, with a few modifications:

void AttackPlayer(Weapon * weapon) 
{
// C++ doesn't support using string literals in switch
// statements so we have to use a different variable
// type, such as an integer
switch (weapon.type)
{
case 0:
ShootArrow(weapon);
break;

case 1:
MeleeAttack(weapon);
break;

default:
IdleAnimation(weapon);

}

}

But in this particular case, we are just calling a different function based on the value, with each of the functions being some kind of attack. Instead, we should make use of polymorphism and have the code automatically do the correct thing:

  class Weapon 
{
public:
virtual void Attack()
{
// Do nothing
};
};

class Bow : Weapon
{
public:
virtual void Attack()
{
// Attack with Bow
};
};

void AttackPlayer(Weapon * weapon)
{
weapon->Attack();
}

Now whenever we call AttackPlayer, it will do the correct thing automatically.

Just remember that creating complex behavior leads to ugly code being written and increases the likelihood of bugs. If you forget about a condition that needs to be there, your game hopefully would break, letting you know there is a problem, but it could not do anything. Then, when you find your game crashes down the road, your life becomes a lot more complex and your game could become unplayable or just plainly not fun.

Robert Elder has a link of the subject which I think explains the kind of crazy things that you can do with conditional statements, which would almost certainly get you fired: http://blog.robertelder.org/switch-statements-statement-expressions/.

Don't lose sleep over having conditionals in your code, but make sure that you only include them when you actually need them there. As you continue coding, you'll have a better idea as to when it's a good idea or not, but it is something to keep in mind.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.45