Code Optimization -
CoDeZ - 04.07.2012
Hello guys , i am helping a friend of mine with scripting and stuff
But the old scripter made it the script messy and unoptimized
Example:
As seen up there its all if statements into each other
The question is , how can i optimize this code?
or it will just work fine this way?
Re: Code Optimization -
TomTroX - 04.07.2012
if( .. || .. || .. )
{
}
Re: Code Optimization -
Revo - 04.07.2012
it will work like that, and generally will be easy to see what is done.
Although it may be better to use
pawn Код:
if(value > 2000) {
if(!IsPlayerAdmin && AdminLevel[playerid] == 0) {
BanEx(playerid, "cashhacking");
}
}
Just for the example of it, sometimes multiple if's are better than combining them, sometimes they're not.
Re: Code Optimization -
CoDeZ - 04.07.2012
Quote:
Originally Posted by TomTroX
if( .. || .. || .. )
{
}
|
Give me an example please?
Quote:
Originally Posted by BuuGhost
it will work like that, and generally will be easy to see what is done.
Although it may be better to use
pawn Код:
if(value > 2000) { if(!IsPlayerAdmin && AdminLevel[playerid] == 0) { BanEx(playerid, "cashhacking"); } }
Just for the example of it, sometimes multiple if's are better than combining them, sometimes they're not.
|
You haven't seen the script , they are like 10 if statments into each other.
Re: Code Optimization -
ReneG - 04.07.2012
Control Structures
Re: Code Optimization -
[MM]RoXoR[FS] - 04.07.2012
Quote:
Originally Posted by CoDeZ
|
Actually, I like this way as it is easy to find / edit.
He did it correctly (Tab within a Tab)
This would be a bad code though
Re: Code Optimization -
Vince - 04.07.2012
I generally don't do nested ifs. Especially not 10 levels deep. Instead of doing this:
pawn Код:
if(foo())
{
if(bar())
{
// possibly 100 lines of code
}
else return SendClientMessage(playerid, -1, "Not Bar");
}
else return SendClientMessage(playerid, -1, "Not Foo");
I do this:
pawn Код:
if(!foo())
return SendClientMessage(playerid, -1, "Not Foo");
if(!bar())
return SendClientMessage(playerid, -1, "Not Bar");
// possibly 100 lines of code
Re: Code Optimization -
CoDeZ - 04.07.2012
Quote:
Originally Posted by Vince
I generally don't do nested ifs. Especially not 10 levels deep. Instead of doing this:
pawn Код:
if(foo()) { if(bar()) { // possibly 100 lines of code } else return SendClientMessage(playerid, -1, "Not Bar"); } else return SendClientMessage(playerid, -1, "Not Foo");
I do this:
pawn Код:
if(!foo()) return SendClientMessage(playerid, -1, "Not Foo");
if(!bar()) return SendClientMessage(playerid, -1, "Not Bar");
// possibly 100 lines of code
|
Thank you Vince, i've rescripted it using your method and it looks easier now and its optimized
Re: Code Optimization -
ikey07 - 04.07.2012
You actualy deoptimized it^^, your first post showed how should be write the script techinacly correct, while your last one shows how to fuck up the script.
But its your script

if you like to live in messy room, I dont mind :P
Re: Code Optimization -
CoDeZ - 04.07.2012
there are 23 if statments inside each other...
and the old scripter didn't write it like i wrote it in at my first topic
he did it like that
pawn Код:
if(){
if(){
if(){
//20 if statments here
}}}}}}}}}}}}
i optimized it so you guys can understand my question