I see alot of people having un-clean and messy codes, therefore im gonna create this tutorial to help those people.

Description
In this tutorial you will be able to learn how to create more tidy, clean, and read-able code in your script.

Part 1 - Indentation
You probably know how indentation works, but i decided to include this anyways for any people who does not.
Some people bypass the warnings caused by bad indentation by using a piece of code.
Even though that will remove the warnings, your code will be hard to read if you have everything like this:
Code:
CMD:toggle(playerid, params[])
{
if(sscanf(params, "s[14]", params))
{
SendClientMessage(playerid, -1, "/toggle [option]");
SendClientMessage(playerid, -1, "Options: PM");
}
if(!strcmp(params, "pm", true))
{
switch(pm[playerid])
{
case 0:
{
pm[playerid] = 1;
SendClientMessage(playerid, -1, "You have toggled PM's off.");
}
case 1:
{
pm[playerid] = 0;
SendClientMessage(playerid, -1, "You have toggled PM's on.");
}
}
}
return 1;
}
More people using this method also looses track of where to put brackets, as you cannot compare it anymore.
Normally on the spacings you can see how all the brackets affects your code, thereby you are able to see if you are missing one somewhere.
Code:
CMD:toggle(playerid, params[])
{
if(sscanf(params, "s[14]", params))
{
SendClientMessage(playerid, -1, "/toggle [option]");
SendClientMessage(playerid, -1, "Options: PM");
}
if(!strcmp(params, "pm", true))
{
switch(pm[playerid])
{
case 0:
{
pm[playerid] = 1;
SendClientMessage(playerid, -1, "You have toggled PM's off.");
}
case 1:
{
pm[playerid] = 0;
SendClientMessage(playerid, -1, "You have toggled PM's on.");
}
}
}
return 1;
}
After each opening bracket, you should go 4 spaces forward (equivalent to pressing TAB once).
And after each closing bracket, you should go 4 spaces backwards.

If you have alot of code that needs to be moved forwards or backwards, and you do not want to "TAB it out" on each line, you can simply mark the whole code,
and press TAB to move it forwards, or press TAB while holding SHIFT to move it backwards.

Part 2 - Use of brackets - 1/2
Using brackets can be quite straight-forward, but even though you might be using them right, you might not need them at all.
Here's an example of over-used brackets:
Code:
CMD:pm(playerid, params[])
{
new targetid, message[128];
if(sscanf(params, "us", targetid, message))
{
SendClientMessage(playerid, -1, "/pm [playerid] [message]");
return 1;
}
if(pm[playerid] == 0)
{
SendClientMessage(playerid, -1, "Your PM's are turned off.");
}
else if(pm[targetid] == 1)
{
SendClientMessage(playerid, -1, "Your PM's are turned on.");
}
else
{
SendClientMessage(targetid, -1, "PM Received.");
SendClientMessage(playerid, -1, "PM Sent");
}
return 1;
}
As you can see, that will work fine, but there's no need for all those brackets.
So let's try to clean this code up a little.
Let's take a look at the first piece of the code:
Code:
if(sscanf(params, "us", targetid, message))
{
SendClientMessage(playerid, -1, "/pm [playerid] [message]");
return 1;
}
The first mistake, is that returning 1, is the same as returning SendClientMessage.
Code:
if(sscanf(params, "us", targetid, message))
{
return SendClientMessage(playerid, -1, "/pm [playerid] [message]");
}
Therefore that code will work just like the other.
But it can be improved further, as using brackets to enclose 1 function is not necessary.
Therefore you can do this aswell:
Code:
if(sscanf(params, "us", targetid, message)) return SendClientMessage(playerid, -1, "/pm [playerid] [message]");
And it would work just like the first example.
As you can see, it only takes up 1 line, where-as the first example took up 5 lines, and it works just as well as the first one too.

So now we can do the same with the rest of the command aswell, as most of it is brackets enclosing single functions.
Code:
CMD:pm(playerid, params[])
{
new targetid, message[128];
if(sscanf(params, "us", targetid, message)) return SendClientMessage(playerid, -1, "/pm [playerid] [message]");
if(pm[playerid] == 0) SendClientMessage(playerid, -1, "Your PM's are turned off.");
else if(pm[targetid] == 1) SendClientMessage(playerid, -1, "Your PM's are turned on.");
else
{
SendClientMessage(targetid, -1, "PM Received.");
SendClientMessage(playerid, -1, "PM Sent");
}
return 1;
}
As you can see, this is alot shorter than the first example.
But with this code, we need to use brackets around it, making the "else" trigger multiple functions.
Code:
else
{
SendClientMessage(targetid, -1, "PM Received.");
SendClientMessage(playerid, -1, "PM Sent");
}
That will make the else work fine, but doing this:
Code:
else SendClientMessage(targetid, -1, "PM Received."); SendClientMessage(playerid, -1, "PM Sent");
Will not work correctly, as the editor sees it like this:
Code:
else SendClientMessage(targetid, -1, "PM Received.");
SendClientMessage(playerid, -1, "PM Sent");
Therefore having only the "else" trigger one of the SendClientMessages, while the other get's triggered no matter what.

Part 3 - Use of brackets - 2/2

Brackets can be used in different ways, one way you can use them is to do something like this:
Code:
CMD:pm(playerid, params[])
{
new targetid, message[128];
if(sscanf(params, "us", targetid, message)) return SendClientMessage(playerid, -1, "/pm [playerid] [message]");
if(pm[playerid] == 0) SendClientMessage(playerid, -1, "Your PM's are turned off.");
else if(pm[targetid] == 1) SendClientMessage(playerid, -1, "Your PM's are turned on.");
else
{
SendClientMessage(targetid, -1, "PM Received.");
SendClientMessage(playerid, -1, "PM Sent");
}
return 1;
}
As we have done in the rest of the tutorial, but you can use them in a different way.
You can place them on the same line as you use functions, here's an example:
Code:
[pawn]CMD:pm(playerid, params[]) {
new targetid, message[128];
if(sscanf(params, "us", targetid, message)) return SendClientMessage(playerid, -1, "/pm [playerid] [message]");
if(pm[playerid] == 0) SendClientMessage(playerid, -1, "Your PM's are turned off.");
else if(pm[targetid] == 1) SendClientMessage(playerid, -1, "Your PM's are turned on.");
else {
SendClientMessage(targetid, -1, "PM Received.");
SendClientMessage(playerid, -1, "PM Sent"); }
return 1; }
[/pawn]
As you can see, this shortens your code alot when using advanced codes, but it does make your code harder to read, and might also make you miss brackets as they're not that standing on their own lines for you to see anymore.

Part 3 - Operators - "==, >, !, &&, ||"
Short explanations:
(something) == (something else) - Something equal to something else.

(something) > (something else) - Something higher than something else.

(something) < (something else) - Something lower than something else.

(something) && (something else) - Something and something else.

(something) || (something else) - Something or something else.

"!" is a bit special, it cannot be used as above, but can be used like this:

(something) != (something else) - Something not equal to something else.

!(variable/function) - Checks if a variable is set to 0, or a function returns 0.

Some people create long codes to do the same as a simple ">" would do, so remember to have these in thoughts.
Especially the "!", as in functions like strcmp it can help to shorten codes.
Without "!":
Code:
if(strcmp(params, "pm", true) == 0)
With "!":
Code:
if(!strcmp(params, "pm", true))

Part 4 - Custom functions
There are functions that a_samp gives you by default, like SetPlayerHealth and such, but if you didn't know, you can create your own.
By this way you can massively tone down your script, especially if you use alot of the same code different places, you can just put it into a simple function and use that.
Here's an example of a custom GivePlayerMoney function:
Code:
SetPlayerMoney(playerid, amount)
{
ResetPlayerMoney(playerid);
return GivePlayerMoney(playerid, amount);
}
Then if we want to set a player's money to 100, simply do this:
Code:
SetPlayerMoney(playerid, 100);
This may not be the most advanced code, but you get the idea.

Part 5 - Defines
Using defines is a very good way to make your code better, and even more read-able.
A define is just a way to rename something to something else.
Etc. using red color in SendClientMessage, instead of doing this:
Code:
CMD:redmessage(playerid, params[]) return SendClientMessage(playerid, 0xFF0000AA, "This message is red");
Again, you do not need brackets in a case like this.
But instead of using many different 0x?? ?? ?? which you cannot organize very well, you can just use this:
Code:
#define COLOR_RED 0xFF0000AA // Add at the top of your script

CMD:redmessage(playerid, params[]) return SendClientMessage(playerid, COLOR_RED, "This message is red");
Thereby you won't loose track if you use red colors that are slightly different, and if you need to change the red color, you can just change the define.

That's all i have for now, i know alot of people who want to clean and tidy up their code, so i hope it will help you.
I will update this tutorial if i find any mistakes or other improvements.
__________________
[Tutorial] Custom Playerdamage system
Desert Eagle's dealing too much damage, you want one-hit sniper kills, or damage based on body parts? Check this out.