Jump to content

Coding guidelines


Kenix

Recommended Posts

Before you put the code, better read this items:

  • Standard design code
    Presence total style programming facilitate understanding source code. In any control structures, operators be is indented. Binary operators should frame spaces.
  • Comments
    Strive comment each needed row of the code. It's much easier to read.
  • Definition of variables, events, handlers
    Order:
    • Events ( addEvent )
    • Variables ( WARNING:Functions define last! Next define handlers. )
    • Handlers ( addEventHandler,... )

    It's also easier to read code.

    [*]Logs

    If your code not working, use /debugscript 3 in chat or debugscript 3 in console.

    Also all errors will be in the logs.

    Server log:

    Server folder\server\mods\deathmatch\logs\server.log

    Client log:

    Client folder\MTA\clientscript.log

    Do not forget to clean the logs.

    [*]Logically, a non-working code

    If your code refuses to work, not debug messages, not logs, you need check each row of the code.

    Namely:

    • Variables
    • Conditions
    • Return values ​​of functions
    • and etc

    [*]Completely non-working code

    If your code refuses to work, possibly your code has syntax errors.

    Better put debugger in your text program.

    If you not know, how to put debugger in text program , better download ready-made program with debugger:

    [*]Completely non-business resource

    If your resource refuses to working, check meta.xml.

    Possibly, meta.xml has syntax errors and so your resource can't be working in any case.

    More info about meta.xml

    https://wiki.multitheftauto.com/index.php?title=Meta.xml

    [*]Example

    local Some =  
    { 
        'somevalue', 
        'somevalue2' 
    } 
      
    function GetSomeValue( ) 
        local stringRandom = aSome[ math.random( #Some ) ] -- We obtain a random value from a table of random key. 
        outputChatBox( 'Random value is: ' .. stringRandom ) 
    end 
    addCommandHandler( 'somevalue', GetSomeValue ) 
    

Original post from me in russian section: viewtopic.php?f=141&t=40703

Edited by Guest
Link to comment

My 3 cents.

Coding guidelines

-Use comments only when certain pieces of code can't be read very easily. This is a WRONG example:

x,y,z = getElementPosition( player ) --We get the position of the player 

That comment is completely useless, because the function is self-explanatory just like the argument.

-Variable names should be very clear.

-INDENT THE CODE.

Link to comment
local sRandom = tSome[ math.random( #tSome ) ] -- We obtain a random value from a table of random key. 

Maybe, someone not understand this.

It is best to write comments.

-INDENT THE CODE.

Who else wants to can align a comments.

x,y,z = getElementPosition( player ) --We get the position of the player 

Ok we know this function return 3 axis ( x,y,z ). Maybe we not know variable player is not element or something.

Edited by Guest
Link to comment
local sRandom = tSome[ math.random( #tSome ) ] 

That doesn't make sense when there is no context.

First of all, "tSome" is very unlikely as a variable name. "player" is very obvious.

You don't get the point of my first comment.

Edited by Guest
Link to comment

Benxamix2,You can write as much as necessary, but comments are need.

Why comments have come up with? :lol:

I use prefix name for variables.

n = number, b = boolean, ..

It also simplifies reading the code, because you know what this type of variable.

P.S I have often seen the names of the variables like ( lol,ga1,rh2,... ) it's not good believe me.

Edited by Guest
Link to comment
pls wots a code

Fine.

-Variable names should be very clear. Good examples are:
player = getLocalPlayer( ) 
allPlayers = getElementsByType( "player" ) 
allObjects = getElementsByType( "object" ) 

And bad examples are:

p = getLocalPlayer( ) 
a_p = getElementsByType( "player" ) 
lolwut = getElementByID( "someobject" ) 

-INDENT THE CODE. Self-explanatory. Good example is:
function someFunction ( player ) 
    setElementHealth( player, 0 ) 
end 

Bad example is:

function myFunction2(player) 
x,y,z=getElementPosition(player) 
outputChatBox(tostring(x)..tostring(y)..tostring(z)) 
end 

Also, I don't see the point on writing extra comments for code that is readable even when you don't know coding.

I mean, who can't understand what does "getElementPosition" does?

Why to write a comment stating that "getElementPosition" gets an element position? It's stupid.

I DON'T MEAN YOU SHALL NOT WRITE ANY COMMENT.

I MEAN YOU SHALL NOT FILL YOUR SCRIPT WITH COMMENTS ABOUT EVERY LINE, SPECIALLY WHEN THOSE LINES ARE VERY CLEAR BY THEMSELVES.

Edited by Guest
Link to comment

1. i believe this should go into tutorials subforum, now that we have one.

2. translation is horrible. i dont know, who stickied it, so i'm not gonna unstick, but...

get someone with proper english knowledge to proof read this, since this is a general (english) section.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...