Raised This Month: $32 Target: $400
 8% 

Avoid duplicating the code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JocAnis
Veteran Member
Join Date: Jun 2010
Old 10-08-2019 , 09:49   Avoid duplicating the code
Reply With Quote #1

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 :/
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 10-08-2019 at 10:07.
JocAnis is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 10-08-2019 , 09:59   Re: Avoid duplicating the code
Reply With Quote #2

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.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
JocAnis
Veteran Member
Join Date: Jun 2010
Old 10-08-2019 , 10:11   Re: Avoid duplicating the code
Reply With Quote #3

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
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 10-08-2019 at 10:11.
JocAnis is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 10-08-2019 , 11:09   Re: Avoid duplicating the code
Reply With Quote #4

Code:
Function1() {     if( get_pcvar_num( cvar1 ) )     {         if( blablabla )         {             Function2();         }     }     else     {         if( tuctuctuc )         {             Function2();         }     } } Function2() {     //do some 20 lines here (same lines as above) }
__________________
hleV is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 10-08-2019 , 11:43   Re: Avoid duplicating the code
Reply With Quote #5

Or

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








CrazY. is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 10-08-2019 , 12:57   Re: Avoid duplicating the code
Reply With Quote #6

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.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Natsheh
Veteran Member
Join Date: Sep 2012
Old 10-08-2019 , 13:53   Re: Avoid duplicating the code
Reply With Quote #7

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 
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 10-08-2019 at 14:00.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
JocAnis
Veteran Member
Join Date: Jun 2010
Old 10-09-2019 , 18:32   Re: Avoid duplicating the code
Reply With Quote #8

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
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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