IF Hater Club: Replace The Code

Non-platform specific questions.

IF Hater Club: Replace The Code

Postby Bee-Ant » Fri Oct 02, 2009 2:45 am

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?
Last edited by Bee-Ant on Sat Oct 03, 2009 10:24 am, edited 1 time in total.
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Fri Oct 02, 2009 8:56 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Fri Oct 02, 2009 9:34 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Bee-Ant » Fri Oct 02, 2009 10:01 am

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
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Fri Oct 02, 2009 11:51 am

I cant wait.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Bee-Ant » Fri Oct 02, 2009 12:57 pm

What if I just send you the whole codes via email?
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Sat Oct 03, 2009 12:30 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Bee-Ant » Sat Oct 03, 2009 3:07 am

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.
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Sat Oct 03, 2009 6:17 am

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?
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Bee-Ant » Sat Oct 03, 2009 7:08 am

Not really, about 200 lines only...
Why?
Everything need checking slower the performance?
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Sat Oct 03, 2009 7:25 am

Yes, checking strings is slower than number checks.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Sat Oct 03, 2009 9:00 am

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?
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Fuzzy » Sat Oct 03, 2009 9:26 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF hater : replace the code

Postby Bee-Ant » Sat Oct 03, 2009 10:20 am

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.
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Fuzzy » Sat Oct 03, 2009 12:04 pm

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!
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Next

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron