SA-MP Forums Archive
Which one is better? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Which one is better? (/showthread.php?tid=622131)



Which one is better? - Micko123 - 19.11.2016

Okay so lets say I have command to create.. I need tome if checks. Should I do it like this
PHP код:
if(IsPlayerConnected(id) && PlayerInfo[id][pAdmin] == && PlayerInfo[id][OrgJoined] == 0)
{
        
//rest of the code
        
return 1;

Or should I go like this
PHP код:
if(!IsPlayerConnected(id))
    {
        
ERROR(playerid"That player is not conneted");
        return 
1;
    }
if(
PlayerInfo[id][pAdmin] > 0)
    {
        
ERROR(playerid"That player is admin");
        return 
1;
    }
if(
PlayerInfo[id][OrgJoined] > 0)
    {
        
ERROR(playerid"That player is already member");
        return 
1;
    } 
Is there any difference in speed??


Re: Which one is better? - iLearner - 19.11.2016

Depends on you, but i'd use first one.

I mean depends on you cuz the first one will work only if all 3 are true, the second one will check each condition.


Re: Which one is better? - SickAttack - 19.11.2016

It does depend, but one if-then should be logically faster.


Re: Which one is better? - Micko123 - 19.11.2016

Yeah it should but i was thinking.. If I do it with one if problem is that I can't send error message if player is admin or if he is in an org...


Re: Which one is better? - iLearner - 19.11.2016

Isnt that why i said... it depends on you.


Re: Which one is better? - setes7 - 19.11.2016

Exactly, there's not a huge speed difference, but you can't send your error messages. I would go with 2nd way, just because I like to keep it organized.


Re: Which one is better? - Vince - 19.11.2016

If you've got (or want) three different messages then the first one won't even be appropriate.

That said, sometimes it is appropriate to bundle multiple checks to avoid nesting too deep. Nothing is more annoying than reading something like this:
PHP код:
if()
{
    if()
    {
        if()
        {
            if()
            {
                if()
                {
                    if()
                    {
                        if()
                        {

                        }
                    }
                }
            }
        }
    }

Yet many people do this and in many cases it isn't even properly indented. Gets even worse when "else return" is added into the mix.


Re: Which one is better? - Micko123 - 19.11.2016

Thank you all guys