[Tutorial] How to make a Tidy script
#1

How to make a Tidy script


Hi, everyone.
Today i'm gonna learn you how to make a tidy and clean script. A lot of pawners (me too) have bad style of writting code. Big problem is in case, if you share this code as free. A lot of programmers have problem read your script. Anyway in future you will have to make tidy and clean script for any case.

At the begining i show you simple script, which can be modified to more easily.
pawn Код:
public OnPlayerSpawn(playerid)
{
    new money = GetPlayerMoney(playerid);
    if (money > 100000)
    {
        new str[50];
        format(str, sizeof (str), "You are rich ( $%i )" money);
        SendClientMessage(playerid, -1, str);
    }
    else
    {
        new str[50];
        format(str, sizeof (str), "You have only $%i" money);
            SendClientMessage(playerid, -1, str);
    }
    return 1;
}
In this script we have 2 time SendClientMessage, which could be lazy to write and repeat the same function. String too. We 2 times create string about 50 chars. So we know that be succes only 1 condition.
So we can change it like that:
pawn Код:
public OnPlayerSpawn(playerid)
{
    new money = GetPlayerMoney(playerid);
    new str[50];
    if (money > 100000)
    {
        format(str, sizeof (str), "You are rich ( $%i )", money);
    }
    else
    {
        format(str, sizeof (str), "You have only $%i", money);
    }
    SendClientMessage(playerid, -1, str);
    return 1;
}
We save line and our work. Script is more easily too.

Second part of this lesson is styling the code.
A lot of people have alignment in code of some char and symbols.

pawn Код:
public OnPlayerSpawn(playerid)
{
    new money = GetPlayerMoney(playerid), str[50];
    if (money > 100000)  format ( str, sizeof  (str ), "You are rich ( $%i )", money );
    else format ( str, sizeof (str), "You have only $%i", money );
    SendClientMessage(playerid, -1, str);
    return 1;
}
Many of you want to save line, but you not thought about styling and reading the code. More beautiful is this code, which is better to read.

pawn Код:
public OnPlayerSpawn(playerid)
{
    new money = GetPlayerMoney(playerid), str[50];
    if (money > 100000)
    {
        format (str, sizeof  (str ), "You are rich ( $%i )", money );
    }
    else
    {
        format (str, sizeof (str), "You have only ( $%i )", money );
    }
    SendClientMessage(playerid, -1, str);
    return 1;
}
Now you know, what do you have in if condition and stage else and it looks better.


Third part of this lesson are variables.
Variables are unnecessary things in every programming language. In PAWN too. Variables can be named as you want. Classic vars are 'var[]'. Different are enums 'var[][enum]'. In every case you should make name of vars readable and it should express what it doing or what store in. Example you shouldn't use playerMoney and save players score in. Next part are lowercase and uppercase. A lot of expert programmer use 'Camel Method'. It is example 'playerInfo' or 'someVar' (second word have uppecase first char).

pawn Код:
new pInfo, playerInfo...
Fourth part is cycle
We all know what cycle is (for, while...). And we know that it uses varbiable too. Often is used i. It is basically and most used name of variable in cycle. But what we have to do if we have cycle in cycle? - Right names and sequence of variable names are i, j, k, l.
Example here:
pawn Код:
for (new i=0;i<MAX_PLAYERS;i++)
{
    for (new j=0;j<MAX_TEAMS;i++)
    {
        // make code here
    }
}

I am surprised that you've read this tutorial. Thank you for read.

In future i may add more informations.
Reply


Messages In This Thread
How to make a Tidy script - by ylleron - 07.08.2011, 15:59
Re: How to make a Tidy script - by Kush - 07.08.2011, 23:30
Re: How to make a Tidy script - by Shockey HD - 07.08.2011, 23:53
Re: How to make a Tidy script - by ylleron - 08.08.2011, 07:24
Re: How to make a Tidy script - by Godhimself - 08.08.2011, 07:34
Re: How to make a Tidy script - by ylleron - 08.08.2011, 08:16
Re: How to make a Tidy script - by Godhimself - 08.08.2011, 08:23
Re: How to make a Tidy script - by ylleron - 08.08.2011, 08:28
Re: How to make a Tidy script - by Godhimself - 08.08.2011, 08:29
Re: How to make a Tidy script - by Kush - 08.08.2011, 08:31

Forum Jump:


Users browsing this thread: 2 Guest(s)