Kenix Posted February 29, 2012 Share Posted February 29, 2012 (edited) Before you put the code, better read this items: Standard design codePresence total style programming facilitate understanding source code. In any control structures, operators be is indented. Binary operators should frame spaces. CommentsStrive comment each needed row of the code. It's much easier to read. Definition of variables, events, handlersOrder: 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. Notepad++viewtopic.php?f=91&t=30077 If you not know, how to put debugger in text program , better download ready-made program with debugger: LuaEdithttp://sourceforge.net/projects/luaedit ... t/download MTA Script editorviewtopic.php?t=24834 [*]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 May 18, 2012 by Guest Link to comment
12p Posted February 29, 2012 Share Posted February 29, 2012 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
Kenix Posted February 29, 2012 Author Share Posted February 29, 2012 (edited) 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 February 29, 2012 by Guest Link to comment
12p Posted February 29, 2012 Share Posted February 29, 2012 (edited) 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 February 29, 2012 by Guest Link to comment
Xeno Posted February 29, 2012 Share Posted February 29, 2012 My 3 cents.-Variable names should be very clear. -INDENT THE CODE. pls wots a code Link to comment
Kenix Posted February 29, 2012 Author Share Posted February 29, 2012 (edited) Benxamix2,You can write as much as necessary, but comments are need. Why comments have come up with? 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 May 17, 2012 by Guest Link to comment
12p Posted February 29, 2012 Share Posted February 29, 2012 (edited) 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 March 8, 2012 by Guest Link to comment
Kenix Posted February 29, 2012 Author Share Posted February 29, 2012 Do you own point of view on this matter. Read my last post. Link to comment
12p Posted February 29, 2012 Share Posted February 29, 2012 Read this post, section "Clean code tips and tricks" http://gmc.yoyogames.com/index.php?showtopic=511359 There is a tip called "DO comment, DON’T overcomment" Last 2 comments in your example code shown at the first post are really unneeded. But the first line (about the table) is so unclear that it's perfectly fine. Link to comment
Kenix Posted February 29, 2012 Author Share Posted February 29, 2012 There is a tip called "DO comment, DON’T overcomment" This just example. Link to comment
Aibo Posted March 8, 2012 Share Posted March 8, 2012 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now