Thursday, September 19, 2013

Making Your Code Professional

Good Coding Practice

Hello, my name is Cameron Stickley ( LOLFUNNY12345 | @roblox_tweeter ). You probably don't know me but I was a Roblox Builder and was very active but rarely use Roblox now. I have turned my head away from building and now spend my spare time using code to do complex maths algorithms and make programs to test my abilities. I know Pascal, Python and some RBX.Lua. 

When you learn computer science and research code structures you start to take on the ideas which go with them, these ideas being Code Structure. There are 4 main ideas I want to describe how to use properly.


Some languages (inlcuding Python) don't use "end" to end a function or procedure instead they will use "white space". This means that the compiler will check to see if the next line of code has been indented to a same or greater distance and then skip over it. If it's not indented as far it is "out of the function" this means that the compiler thinks it is the continuation of the code. Indentation also makes your code look neater, cleaner and more organised, it helps with cleaning up or removing errors that occur in the code and mean you can edit code more simply.


You may be used to people occasionally placing comments in code as jokes, for instance as you read through "Person299's Admin Commands V2" wrote in RBX.Lua on the Roblox platform you will see him commenting just about a bug on his computer screen. This is not a useful use of comments, they are meant to be used to explain what is going on in that section of code and why. This means that you can debug the code easier and not affect the rest of the source code.


Functions and procedures allow you to call a predefined set of code with certain parts of information entered to affect the code, you don't always need a parameter in a function but most do require one. For instance in RBX.Lua the most used function is probably wait([number]) not many people think of this as a function but it is, it stops the code for a selected amount of time and when there is no parameter or argument entered ( wait() ) the value is set to the default you can change in roblox studio settings (I think it's 0.0333333 seconds). Functions also make it easier to debug as you only have one line to change with an error rather than as many times as it appears. This allows easier debugging and neater code.


The 2 main variable problems are you need to define variables at the start of code to reset the value to prevent errors due to dividing by zero etc. The next problem is sensible names; you may think good coders use "for i = 1, 3 do" in RBX.Lua (sorry if code is incorrect syntax, been using varying languages recently) but this is the sign of an amateur. "i" is not a sensible variable name and does not help. Try using "count" rather than "i". 

Following these rules should help you become a better coder, thank you for reading.