Which one is better?
#1

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

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

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

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

Isnt that why i said... it depends on you.
Reply
#6

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

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

Thank you all guys
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)