User:Merlin11188/Good Code

From Legacy Roblox Wiki
Jump to navigationJump to search
In Lua, a language that isn't whitespace sensitive, code easily become an unreadable mess. How can we combat this problem? By spreading the word of good code. Also, you are 13x more likely to get a response in Scripting Helpers if your code is legible. Note: These are merely suggestions; as stated earlier, Lua isn't whitespace sensitive so these suggestions aren't required.

Legibility

"Write your code as if the guy who ends up maintaining it is a violent psychopath who knows where you live." Alright, I know that doesn't really apply to ROBLOX, but it's good advice anyway. So, here's how you can write neat-looking code:

Use Whitespace Characters

Number one rule of good code:

  • Use whitespace!

Hmmm? Where should we use these characters?

  • Before and after relational operators!:
x = 4
y = "Hello " .. "merlin!"
print(x > 3)
true
  • Personally, I think that xLEGOx's idea of spaces with arithmetic operators is nice. With his way, you only put spaces in between terms (plus or minus):
y = x + 4
y = x*2 - 4
y = x*2/3*4 + 3
No output
  • When listing table items, arguments, variable declarations, parameters, etc.:
x = {3, 4, 5, 6, 7}
doFunction("a", "b", "c", "d")
x, y, z = 3, 4, 5
print(x, y, z)
3 4 5
  • Put each statement on a new line:
y = x + 3
print("Hello")
Hello
  • Add one indentation for each code block (I'm aware you can't do this on the forums):
x = 3
if x == 3 then
    print(x)
end
3

Comment your Code

Alright, this is one most people have a problem with (including me). Commenting your code is very important. Here are some tips:

  • At the top of the script, in a comment, make a function prototype. Here's an example of a function prototype:
--Add(number, number) -- The name of the function, and the data types of the parameters

function Add(xVal, yVal)
	return xVal + yVal
end
No output
  • Comment all your functions, with the values to be returned, etc.:
function Add(xVal, yVal)
	--[[Parameters: xVal: number; yVal: number;
	Description: Add xVal and yVal
	Return Value: xVal + yVal]]
	return xVal + yVal
end
No output
  • Don't comment line-by-line. That's irritating. Here's a finishing example:
--[[
f(number)
]]

function f(Number)
--[[Parameters: Number: number;
Description: To perform mathematical operations on Number
Return Values: Number^10]]
	Number = (Number^10)/(Number^5)
	return Number^2
end

function g() -- Return a random number
	math.randomseed(tick()) -- or os.time() in regular Lua
	return math.random(7)
end

x = g() -- Generate a random number
print(f(x))
A random number

In certain places commenting can be annoying, so use good judgement.

Name your Variables

  • Do not name your variable something like a keyword, library name, or pre-built function! If in the case of a library name or pre-built function, it prevents you from even using it.
string = "Hi" -- BAD. `string` refers to the string library.
greeting = "Hi" -- Good. This avoids overwriting the string library.
No output
  • Name your variables something descriptive. This is rather important. Always write your code so that it can be understood without comments.
names = {"Merlin11188", "SNCPlay42", "Meelo", "Mattchewy"}
numericalIndex = 3
No output
  • This one is the general preference of many programmers: camelCase (or alternatively lower camelCase). This is the capitalization of your variable identifiers. When you have variables that are multiple words, you cannot put spaces; instead, you capitalize the first letter of every word (except for the first) to show separation. Let me show you what I mean:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphabetSoup = "Delicious"
theHyperbolicTangentOfX = 3
No output

Efficient Code

Your code should be efficient, so long as you don't have to sacrifice legibility. What is efficient code? Efficient code is short, powerful code that does what you want.

  • Shorter is (usually) better:
numbers = {2, 3, 4}
for index, value in pairs(numbers) do
if index == 3 then
print(value)
end
end
4

That code was terribly inefficient! Here's what would be much better:

y = {2, 3, 4}
print(y[3])
4

7 lines of code was shortened into 2, and the second one is, actually, more readable.

  • If you have more than 3 if...elseif statements, you're doing something wrong. Let me show you what I mean:
x = 3
if x == 1 then
print("Hi")
elseif x == 2 then
print("Hi!")
elseif x == 3 then
print("Hello")
elseif x == 4 then
print("Hello!")
end
Hello

That's a bad way of doing it. Here's a much better way:

x = 3
greetings = {"Hi", "Hi!", "Hello", "Hello!"} 
print(greetings[x])
Hello

The second example is much shorter, much easier to understand, and much nicer to look at.