Snippets

Leslie Krause My Coding Style Conventions (Lua)

Created by Leslie Krause last modified
-- no space between name and arguments list of a function call

foo( bar )

-- always one space after commas and within parentheses and braces

foo = { 1, 2, 3 }

-- always one space surrounding binary operators

bar = ( foo + 2 ) / 8

-- always one space before arguments list of an anonymous function definition

foo = function ( bar )

-- single-line if-statements are okay for short circuit logic only

if not foo then return end

-- unwieldy or complex equations should be broken into multiple parts

local length = string.len( bar ) + string.len( foo )
local result = math.min( 10, math.min( 255, length ) )

-- don't put multiple assignments on one line (except for function returns)

local a, b = 1, 2  -- avoid this!

local a = 1  -- much better
local b = 2

-- use hanging indents to split lengthy statements, preferably at delimiters:
-- 1) with function calls, indent between parentheses with first parameter
--    either on same line, or next line for optimum readability.

-- best choice
print( string.format( "Lots of great test here %s, and more text %d",
	list_of_names[ user_id ].first_name,
	sessions[ user_id ].length
) )

-- second best choice
print( string.format(
	"Lots of great test here %s, and more text %d",
	list_of_names[ user_id ].first_name,
	sessions[ user_id ].length
) )

-- third best choice
print(
	string.format(
		"Lots of great test here %s, and more text %d",
		list_of_names[ user_id ].first_name,
		sessions[ user_id ].length
	)
)

-- it is also acceptable in some circumstances to retain the trailing 
-- parenthesis on the last line, for compactness, as long as it doesn't 
-- diminish readability.

print( string.format( "Lots of great test here %s, and more text %d",
	list_of_names[ user_id ].first_name,
	sessions[ user_id ].length ) )

-- 2) with table definitions, indent between braces, with opening brace on
--    first line. alway include a trailing comma when using this format.

local blocked_usernames = {
	"fred",
	"wilma",
	"betty",
	"dino",
	"barney",
}

-- it is also acceptable to place all elements on a single indented line, but 
-- only if they fit. in this case, leave off the trailing comma.

local blocked_usernames = {
	"fred", "wilma", "betty", "dino", "barney"
}

-- avoid excessively long lines over 80-100 characters (except for grouping)

-- always insert a blank line between function definitions (except for grouping)

-- only use tabs for indentation, never spaces (and get a proper editor too)

-- always subdivide code segments into logical units by a single line 

-- insert a long row of dashes to further separate very large segments of code

-- readability and maintainability is more important than efficiently structured code

Other useful coding conventions by Linus Torvalds, most of which I agree with ;D
https://www.kernel.org/doc/html/latest/process/coding-style.html

Using closures as a means of object oriented programming
http://www.troubleshooters.com/codecorn/lua/luaoop.htm

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.