AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   breaking up an if statement into multiple if statements? (https://forums.alliedmods.net/showthread.php?t=89372)

diamond-optic 04-05-2009 15:04

breaking up an if statement into multiple if statements?
 
now i just wanna get an educated response on this cause i honestly am not sure if theres any benefit of one over the other.. and if there is, if its really enough of a benefit to make any difference

but say you have an if statement like this:
Code:
if(!this || !that || !something || !etc || whatever || evenmore || !yadayada)     return FMRES_IGNORED


im being told by someone that this is a better way to do it:
Code:
if(!this || !that)     return FMRES_IGNORED if(!something || !etc)     return FMRES_IGNORED if(whatever || evenmore)     return FMRES_IGNORED if(!yadayada)     return FMRES_IGNORED



so just hoping to get some clarification on this...

ConnorMcLeod 04-05-2009 15:11

Re: breaking up an if statement into multiple if statements?
 
First one is just fine, if this == 0, other statements won't be checked.

fysiks 04-05-2009 15:17

Re: breaking up an if statement into multiple if statements?
 
I was going to say something to that effect but I was looking for where I read it :). It was a post by Exolent.

Bugsy 04-05-2009 15:18

Re: breaking up an if statement into multiple if statements?
 
A switch is better to use when you have if statements like this:

PHP Code:

if ( == 
    
//do something
else if ( == )
    
//do something 
else if ( == )
    
//do something 

If you have multiple if checks it is better to use a switch statement.

PHP Code:

switch ( var )
{
    case 
1//do something
    
case 2,3,4
    {
        
//do stuff
    
}
    case 
5//do something
    
case 6,7,8//do stuff
    
default: //Nothing hit, do stuff



diamond-optic 04-05-2009 15:20

Re: breaking up an if statement into multiple if statements?
 
alright...


and yeah i usually use switches if they seem appropriate..

but with an if statement such as this for example i figured one if statement would be fine
Code:
if(!get_pcvar_num(p_whatever) || !is_user_connected(id) || is_user_bot(id) || !is_user_alive(id) || ent > maxplayers || g_round_restart)     return HAM_IGNORED

stupok 04-05-2009 15:20

Re: breaking up an if statement into multiple if statements?
 
If you're hoping to avoid checking the values, make sure you put them in the following order:

if( !most_likely_to_be_zero || ... || ... || ... || !least_likely_to_be_zero )

or, more clearly:

if( most_likely_to_be_true || ... || ... || ... || least_likely_to_be_true )

EDIT: wow, a lot of posts at once O_O

ConnorMcLeod 04-05-2009 15:21

Re: breaking up an if statement into multiple if statements?
 
I usually put statements that doesn't require a native call before the other one.
In the last example you gave, i would for example put "ent > SETTING_MAXPLAYERS || g_round_restart" at first.

diamond-optic 04-05-2009 15:24

Re: breaking up an if statement into multiple if statements?
 
i always kinda had a feeling that once it hit something that caused the statement to be true or whatever that it wouldnt bother checking the rest of it since its going to be true anyway..

but for some reason i never thought of ordering it like that.. good call! :D


All times are GMT -4. The time now is 02:17.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.