-
Notifications
You must be signed in to change notification settings - Fork 435
CodeConventions
Vexatos edited this page Jul 13, 2014
·
5 revisions
For those that don't like images: the wiki has moved to a new place, http://ocdoc.cil.li/.
This wiki will no longer be updated.
If you'd like to contribute code and save me the work of reformatting stuff (which will also make it more likely for it to be pulled!) please try to stick with the following conventions:
- Be consistent.
- Indent using spaces, two wide. Do indent properly.
- Try to limit width of the code to 80 chars.
- Don't add spaces between braces/brackets and what's in them.
- Do use brackets in functions calls even if it's not necessary.
- Name variables for what they are, don't include type markers in them, i.e. do not use Hungarian notation.
- Nice to have: sort your requires alphabetically (OCD!)
- Only comment if it's something complicated/non obvious. Keep in mind that comments increase file size, which increases the amount of RAM required to run your program!
- If you have to validate arguments, use the built-in
checkArg
method. Homogenous error messages are a good thing! Its usage ischeckArg(n, value, type1, ...)
, wheren
is the number of the argument,value
is the value of the argument andtype1
and so on are the allowed types for the argument, as retrieved viatype(value)
. The number is used in the error message like so: "bad argument #n (type1 expected, got type(value))". So for example, to require the first argument to be a number you'd docheckArg(1, arg, "number")
.
Bad:
function f(sArg1 , ... )
assert(type(sArg1)== "string", "me wants a strign!")
if sArg1 then
local nResult = 1
--do some more stuff
return nResult
end
end
if f ( "a" ) ==1 then
print"asd"
end
Good:
function f(name, ...)
checkArg(1, name, "string")
if name then
local result = 1
-- We extrapolate the b-spline of the non-euclidean space to
-- determine the fraction of potential failures encountered.
return result
end
end
if f("a") == 1 then
print("asd")
end