[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
#2

What the hecks this?
Reply
#3

Quote:

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

Im Surprised you made this tutorial...
Reply
#4

Quote:
Originally Posted by Shockey HD
Посмотреть сообщение
Im Surprised you made this tutorial...
Thank you
Reply
#5

I hope the bbcode is making the indentaion from the first bracket/s 8 lines..Because if it wasnt, its a un-tidy tutorial because the indentation is wrong. Pawn uses 4 lines, i forget what but something is coded in pawno to stop 4 line indentation warnings for c#.(Something i read a while ago, dont rage reply the correct facts)
Reply
#6

Quote:
Originally Posted by Godhimself
Посмотреть сообщение
I hope the bbcode is making the indentaion from the first bracket/s 8 lines..Because if it wasnt, its a un-tidy tutorial because the indentation is wrong. Pawn uses 4 lines, i forget what but something is coded in pawno to stop 4 line indentation warnings for c#.(Something i read a while ago, dont rage reply the correct facts)
I do not understand you a lot, sorry.

Added new part - Cycle
Informations about creating cycles
Reply
#7

You code like this:

pawn Код:
if(statement(here))
{
    if(statement(here))
    {
        if(statement(here))
        {
            //code here
        }
    }
}
Not like this:

pawn Код:
if(statement(here))
{
        if(statement(here))
        {
            if(statement(here))
            {
                //code here
            }
        }
}

EDIT:

returns were wrong.
Reply
#8

Quote:
Originally Posted by Godhimself
Посмотреть сообщение
You code like this:

pawn Код:
if(statement(here))
{
    if(statement(here))
    {
        if(statement(here))
        {
            //code here
        }
    }
}
Not like this:

pawn Код:
if(statement(here))
{
        if(statement(here))
        {
            if(statement(here))
            {
                //code here
            }
        }
}

EDIT:

returns were wrong.
it set editor and not me. Here i can't use TAB.
Reply
#9

Quote:
Originally Posted by ylleron
Посмотреть сообщение
it set editor and not me. Here i can't use TAB.
You dont use tab in pawn, or even c#.

Thats for html indentaion lol

Use another line and 4 spaces between brackets/statements.

EDIT: edit.
Reply
#10

No offense, but this is truly useless.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)