Page 1 of 5

IF Hater Club: Replace The Code

PostPosted: Fri Oct 02, 2009 2:45 am
by Bee-Ant
Code: Select all
if(hp<0)hp=0;

Can be replaced with
Code: Select all
hp=max(hp,0);

...
And
Code: Select all
if(hp>maxhp)hp=maxhp;

Can be replaced with
Code: Select all
hp=min(hp,maxhp);

...
But
How to replace both of them with no IFs?

Re: IF hater : replace the code

PostPosted: Fri Oct 02, 2009 8:56 am
by Fuzzy
Bee-Ant wrote:How to replace both of them with no IFs?



I am glad you are thinking like this.
Next what you have to do is replace the variable inside with the other.

Code: Select all
hp=max(0, min(hp, maxhp));


or for a function..

Code: Select all
int numControl(int none, int now, int most)
{
    return max(none, min(now, most);
}


and use it like this

Code: Select all
hp = numControl(0, hp, maxhp);


That is good for all sorts of situations. A more simple function just for hp could be this.

Code: Select all
int hpControl(int hpNOW, int hpMAX)
{
    return max(0, min(hpNOW, hpMAX) ;
}


and use it like this

Code: Select all
hp = hpControl(hp, maxhp);


But you would be better off using the num control one, because it will work in all sorts of situations.

Re: IF hater : replace the code

PostPosted: Fri Oct 02, 2009 9:34 am
by Fuzzy
If you dont like that way, this way works too.

Code: Select all
hp = min(((maxhp+hp) % maxhp), 0);


But it is more of a mess. There are other ways too, but I would choose to use the one that is easiest to read, but not: if.

Re: IF hater : replace the code

PostPosted: Fri Oct 02, 2009 10:01 am
by Bee-Ant
I dont know why Im thinking like this. I think The Mortal Enemy of IF have washed my brain :P

I like the first code,
It makes sense (I can think the result directly)

Just wait, i have a ton of codes :D

Re: IF hater : replace the code

PostPosted: Fri Oct 02, 2009 11:51 am
by Fuzzy
I cant wait.

Re: IF hater : replace the code

PostPosted: Fri Oct 02, 2009 12:57 pm
by Bee-Ant
What if I just send you the whole codes via email?

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 12:30 am
by Fuzzy
Send them here, a few at a time. Its good for other people to see how we do things.

And I like things a bit at a time.

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 3:07 am
by Bee-Ant
Okay...
Could I replace IF for strcmp with switch-case?
Code: Select all
if(!strcmp(name,"bee-ant"))

To
Code: Select all
switch(name){
case 'bee-ant': break;}

Im away to my PC, i cant try it out now.

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 6:17 am
by Fuzzy
Bee-Ant wrote:Okay...
Could I replace IF for strcmp with switch-case?
Code: Select all
if(!strcmp(name,"bee-ant"))


Do you use strcmp lots Bee-Ant?

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 7:08 am
by Bee-Ant
Not really, about 200 lines only...
Why?
Everything need checking slower the performance?

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 7:25 am
by Fuzzy
Yes, checking strings is slower than number checks.

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 9:00 am
by Fuzzy
If you have lots of names...

Code: Select all
// in global code

enum namelist { beeant=0, dst=1, fuzzy=2, makslane=3, pyro=4, billgates = 5 };


// you dont need the =number. It can be used to give different values. you dont
// have to go 0,1,2,3,4,5.... you can skip number or repeat them 1,1,5,6,8,90,90,90


beeant = 0 but 0 does not equal beeant. namelist is not a variable. its a type of variable.

Now you can use it in a switch. You need to make an actor variable. an integer called intname.


Code: Select all
switch(intname) {
    case beeant : break; }


no ' or " is used for this. you cannot use - either. beeant acts like a variable, but it isnt. You cant change its value.You cannot display the name to the player either. If you need to do that, add a global array filled with the names.

Code: Select all
// more global code
char names[] = {'beeant', 'dst', 'fuzzy', 'makslane', 'pyro', 'billgates'};


Code: Select all
strcpy(name, names[intname]);

Make sense?

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 9:26 am
by Fuzzy
One thing that I never see people use is this.

Code: Select all
result = (hp > 45);


in this case, if hp is greater than 45, then result will equal 1. Otherwise it will be 0.

We can write really nice functions with this.

Code: Select all
int isMore(int one, int two)
{
    return( one > two);
}

int isLess(int one, int two)
{
    return (one < two);
}

int isEqual(int one, int two)
{
    return (one == two);
}


or a whole bunch of other functions.

You can see that if and the comparison are not tied together. If is pretty useless without the comparison, but you can use the comparison in other places. In fact, you can do some other neat things with if, but I wont tell you. He is my enemy.

Re: IF hater : replace the code

PostPosted: Sat Oct 03, 2009 10:20 am
by Bee-Ant
I would go with the "enum" code :D
And that comparison code is awesome...
Usually i use
Code: Select all
transp=max(status,1)-status;

To show when it has a status, or hide when it has no status.

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 03, 2009 12:04 pm
by Fuzzy
That is an interesting way to do it Bee-Ant.

Everyone:

When you use if (and sometimes you have to), you are saying "I must be ready for any value in the variable." this is why people have long strings of if() which is really what I dont like. if here and there is ok.

When you use a formula, you are saying "I am in control. The variable does what I say." It is easier that way too, but at first, it is deep thinking.

It really makes a big difference when you have a bug to solve. A bunch of ifs makes it tricky to figure out what might be wrong.

You dont have to be an if hater to participate in this thread. So speak up!