Skip to content

Pull Requests

Person8880 edited this page May 13, 2016 · 8 revisions

Feel free to submit pull requests with features you want added to the mod. However, please try to follow the code style in the mod as it'll make it easier for me to accept and merge in.

Style guide

Indentation should be with tabs, not spaces. Line endings are in the Windows format, make sure you do not change them.

Also, please use spaces after opening brackets, after commas, before closing brackets etc.

-- Avoid this
local Value=math.min(10,15)
-- Do this instead
local Value = math.min( 10, 15 )

Variable naming

Variable names should use camel case starting with an upper case letter. The only exception is numeric for loops, which should be a single lowercase letter starting with i. For example:

Bad:

local variableName = "Hi"
local variablename = "Hi"
local variable_name = "Hi"

function Plugin:eatCake()

end

Plugin.nextCake = 0

for key, value in pairs( Table ) do

end

for Index = 1, #Table do

end

Good:

local VariableName = "Hi"

function Plugin:EatCake()

end

Plugin.NextCake = 0

for Key, Value in pairs( Table ) do

end

for i = 1, #Table do

end

Also, try to avoid massively long variable names, e.g

--No
local DescriptionOfTheMeaningOfLife = "This is a description."
--Yes
local Description = "This is a description."

If pyramids

Please avoid massive if statement pyramids unless there is no way around it. For example:

--Avoid this
function Bleh( Stuff, Cake, Cookie )
    if Stuff then
        if Cake then
            if Cookie then
                print( "Yay." )
            end
        end
    end
end

--Instead do this
function Bleh( Stuff, Cake, Cookie )
    if not Stuff or not Cake or not Cookie then return end

    print( "Yay." )
end

Spacing

Avoid cramming code into the minimum amount of lines, this makes it hard to read.

This is bad:

local NextCake = 0
local function DoStuff()
    local Time = Shared.GetTime()
    if Time < NextCake then return end
    NextCake = Time + 5
    print( "Here, have some cake." )
end

This is better:

local NextCake = 0

local function DoStuff()
    local Time = Shared.GetTime()
    if Time < NextCake then return end

    NextCake = Time + 5

    print( "Here, have some cake." )
end

Function usage

Where possible, please use the functions included with the mod rather than making your own or using NS2 functions. For example, if you want a list of all players, use Shine.GetAllPlayers(), or if you want to convert a time to a nicely formatted string, use string.TimeToString().

For things that require precise timing, use the timer library. For everything included with Shine to aid developing, see here.

Also, where functions are going to be used often, localise the functions instead of calling them from the global scope. For example:

local Notify = Shared.Message
local SharedTime = Shared.GetTime
local StringFormat = string.format

Notify( StringFormat( "The shared time is: %s.", SharedTime() )

String usage

Please avoid using the concatenate operator .. excessively. It is messy and creates garbage data that the string.format and table.concat functions do not.

Bad:

Shared.Message( "The shared time is: "..Shared.GetTime().."\n"..debug.traceback() )

Good:

local Notify = Shared.Message
local StringFormat = string.format

local Time = Shared.GetTime()
local Traceback = debug.traceback()

Notify( StringFormat( "The shared time is: %s.\n%s.", Time, Traceback ) )
Clone this wiki locally