Which the better way
#1

Which the better way:
pawn Код:
if (blaa)
    return blaafu();
if (blaaa)
    return blaaafu();

// etc
or

pawn Код:
if (blaa) {
   blaafu();
}
else if (blaaa)
{
   blaaafu();
}
else
{
 // etc
}
And what is the meaning of return; is it return 1;?
Reply
#2

Anyone?
Reply
#3

It depends on what you wanna check and which code you wanna allow to be executed.

The first example allows both parts of the code to be executed.
Your second example will only execute one of the three parts of code.

I usually use the first method inside commands.
pawn Код:
if (APlayerData[playerid][AdminLevel] < 3) Return SendClientMessage(playerid, 0xFF0000FF, "Only admins level 3 can use this command");
if (GetVehicleID(playerid) == 0) return SendClientMessage(playerid, 0xFF0000FF, "You can only use this command when you're inside a vehicle");

// Do something here if the player is an admin AND he's inside a vehicle
As you see here, this would exit the command immediately when the player is not an admin level 3 or higher.
If the player has at least admin-level 3, the command continues and checks if he's in a vehicle.

If he's not in a vehicle (vehicleid = 0), exit the command as well.
If he's in a vehicle, the command continues even further and the rest of the code will be executed.



If these were in the structure of your second example, only one of the conditions would be checked.


About "return;", I don't know who uses it, but I'm always returning a value to avoid confusion and bugs.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)