ID:265576
 
I took this code from Kivi's thread in Developer How-To because it had made me wonder whether it would be better for you to use another if() statement, or an else statement, and why it is better to do it one way or another. The following is my post in its entirety.

"Kivi wrote:
> mob/verb
> Bunshin_No_Jutsu()
> set category = "Genjutsu"
> if(usr.CControl >= 101)
> var/CCusage = rand(1,10)
> if(CCusage <= 5)
> usr << "[CCusage]/5 Chakra converted"
> usr.Chakra -= CCusage
> usr.CCTrain += 1
> if(usr.CCTrain == 100)
> usr.CControl -= 1
> usr.CCTrain = 0
> return
> if(CCusage >= 5)
> usr << "[CCusage]/5 Chakra converted"
> usr.Chakra -= CCusage
> view(usr) << "[usr]: Bunshin No Jutsu"
> Createclones()
> if(usr.CControl <= 100) //I changed this to else
> usr << "5/5 Chakra converted"
> usr.Chakra -= 5
> view(usr) << "[usr]: Bunshin No Jutsu"
> Createclones()
>

It seems like it would work good, although at one point you used another if() statement when an else statement would more than suffice. I don't know if it would be better to use a second if() statement, or if using the else clause, but I have gotten into the habit of using else."

If anyone has any reasoning on why one way would be better than the other, I would like your opinion.
mob/verb
Bunshin_No_Jutsu()
set category = "Genjutsu"
var/CCusage = rand(1,10)
if(usr.CControl==100)CCusage=5
usr<<"[CCusage]/5 Chakra converted"
usr.Chakra-=CCusage
if(CCusage <= 5)
usr.CCTrain += 1
usr.Createclones()
view(usr) << "[usr]: Bunshin No Jutsu"
if(usr.CCTrain == 100)
usr.CControl -= 1
usr.CCTrain = 0


Cleaned it up. And for that piece of code, that if() is reasily replaced by else. No big dreal.

In response to Mysame
I think you may have missed the purpose of my post. I wasn't asking for someone to clean up the code for me :P

I was just using it as an example for my true question. Are if() staements better to use in situations like that, or else statements? Which is less CPU intensive? Which is more "professional"? You know, stuff like that.
Well, else would work best for that because you would not be running extra checks for things you are already sure of. If you know that CControl not greater than or equal to 101, it must be less than or equal to 100, no need to run a check. Also, with multiple ifs, instead of else ifs, you are running both checks even though it is not possible for both to be true. So in cases like these, you would use "else", but if you had another check, that went along with it, you would want to use "else if"
In response to Satans Spawn
Satans Spawn wrote:
Are if() staements better to use in situations like that, or else statements?
else. Using multiple if's means that you want it to check whether or not each statement is true or not, even if a previous one was true. Using else would stop it when it finds one that is true.

Which is less CPU intensive?
I'd say else statements, since it stops after it finds a statement that is true.

Which is more "professional"?
In this situation? else.
The combination of if statements doesn't work properly there anyway. If it is equal to exactly 100, both code blocks will execute.
In response to Loduwijk
Loduwijk wrote:
The combination of if statements doesn't work properly there anyway. If it is equal to exactly 100, both code blocks will execute.

How do you figure?

       if(usr.CControl >= 101)
...
if(usr.CControl <= 100)


The first says, that if it is 101 or more do whatever, the other says if it is 100 or less do something else.
In response to Satans Spawn
It looks like usr.CControl==100 would trigger both blocks on first glance - I was fooled by it too before I read your post. It's because you have two equal signs there, which is just weird. It's more intuitive if you use one less-than/greater-than symbol, and one greater-than-or-equal-to/less-than-or-equal-to symbol.

That, by the way, is an important reason for using else instead of an extra if; code readability. If you see the else then it's immediately obvious what's going on; with the extra if you actually have to think about it. And, as demonstrated by the mistake that Loduwijk and myself both made, it's very easy to make a mistake!

Also, what if you later want to change the threshold for the first if from 101 to 201? It would be incredibly easy to forget about changing the second if as well, thus introducing a bug.

Bottom line: When else makes sense, use it.