AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Avoid duplicating the code (https://forums.alliedmods.net/showthread.php?t=319056)

JocAnis 10-08-2019 09:49

Avoid duplicating the code
 
hi guys..im interested in 'small' improvement in coding, lets say i have:

Code:

if( get_pcvar_num( cvar1 ) )
{
        if( blablabla )
        {
                //do some 20 lines here
        }
}
else
{
        if( tuctuctuc )
        {
                //do some 20 lines here (same lines as above)
        }
}

i would like to look like this (like hierarchy):
Code:

if( get_pcvar_num( cvar1 ) )
{
        if( blablabla )
}
else
{
        if( tuctuctuc )
}
//do some 20 lines here

but how i undestand, if clausules cant be put like this :/

OciXCrom 10-08-2019 09:59

Re: Avoid duplicating the code
 
Instead of checking if the criteria is true and doing the code there, check if it's false and do "return". Then the code will continue naturally to the 20 lines you need after both checks.

JocAnis 10-08-2019 10:11

Re: Avoid duplicating the code
 
If i undestand you correctly, you are aiming to the 'TEST'...Maybe i gave bad example with it, i have some 'comlpex' IF on both situtaions, so TEST < 0 and TEST > 1 was just a confusing ...see updated post

ps: i saw somewhere, there is option to add 'do:' but cant find it anymore, mby it will do the trick

hleV 10-08-2019 11:09

Re: Avoid duplicating the code
 
Code:
Function1() {     if( get_pcvar_num( cvar1 ) )     {         if( blablabla )         {             Function2();         }     }     else     {         if( tuctuctuc )         {             Function2();         }     } } Function2() {     //do some 20 lines here (same lines as above) }

CrazY. 10-08-2019 11:43

Re: Avoid duplicating the code
 
Or

Code:
Function1() {     if( get_pcvar_num( cvar1 ) )     {         if( !blablabla )             return;     }     else     {         if( !tuctuctuc )             return;     }     // 20 lines here }

OciXCrom 10-08-2019 12:57

Re: Avoid duplicating the code
 
CrazY's post is what I meant. I was too lazy to write down the example from my phone.

hleV - alternatively, he can use "goto" because the code is located in the same function, but the best way IMO is eliminating all the false checks and stopping the function.

Natsheh 10-08-2019 13:53

Re: Avoid duplicating the code
 
PHP Code:

Function1()
{
    if( 
get_pcvar_numcvar1 )  && blablabla)
    {
            
    }
    else if( 
tuctuctuc ) {
            
    }

    
// 20 lines here


Simple.

Also you can use other ways depending on the variables scoop value and the cvar

If you fully duplicating the code means there is no different in body of the code of blabalabla and tuctuctuc why just do it in a one sentence

PHP Code:

  if( get_pcvar_numcvar1 ) && blablabla || tuctuctuc 


JocAnis 10-09-2019 18:32

Re: Avoid duplicating the code
 
Thanks guys...i used
Code:

public func1()
....func2()

func()
...

like 20lines after this 'problem' in the code..probably cuz its coded 3months ago i forgot somehow bout it

'go to:' or 'do:' came to my mind, but it seems more complicated to be added


All times are GMT -4. The time now is 12:26.

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