Code Optimization
#1

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:
pawn Код:
if()
{
      if()
      {
          if()
           {
     
           }
      }

}
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?
Reply
#2

if( .. || .. || .. )
{
}
Reply
#3

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.
Reply
#4

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.
Reply
#5

Control Structures
Reply
#6

Quote:
Originally Posted by CoDeZ
Посмотреть сообщение
Example:
pawn Код:
if()
{
      if()
      {
          if()
           {
     
           }
      }

}
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
pawn Код:
if()
{
if()
{
 if()
{
     
}
}

}
Reply
#7

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
Reply
#8

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
Reply
#9

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
Reply
#10

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)