Code writing and scripting rules

Hi, those are few rules for my style of coding, if you don’t know there are few commonly used rules for naming and writing overall code, soo i wanted to share my style, you can tell if it’s good or not, everyone have preferences, let’s get started!

1. No short or simplified names

This one is to make code more redable for everyone, many devs use those to make work faster,
it’s nothing wrong with them, i used them for 7 years, but now i see that some newer scripters can have a problem with reading some of them.

EXAMPLE:

Normal:

local plr = game.Players.LocalPlayer
local RP = game.ReplicatedStorage

My style:

local player = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

2. Call service at the beginning with GetService

Soo, many devs don’t use GetService at the beginning, they simply call for service by game.Service,
calling it at the beginning allows dev who reads code to understand what script are doing and
which service it uses

Example:
Style:


local ServerStorage = game:GetService("ServerStorage")

-- rest of the code

Workspace is excluded from this, we can simply call:

local Workspace = game.Workspace

or

game.Workspace.Base:Destroy() -- some example code

3. Script’s beginning pattern

This is how we call things in our script from beginning, here are examples:

Local:

-- we call player first
local player = game:GetService("Players").LocalPlayer
-- then we call services
local UserInputService = game:GetService("UserInputService") 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- then we call modules
local Functions = require(script.Parent.Stats)
-- then we call other variables
local playerBase = game.Workspace.EnemyBase
local enemyBase = game.Workspace.EnemyBase
-- then rest of the code

Server:

-- system is often folder with scripts related to each other
local System = script.Parent 
-- then services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game.Workspace
-- then modules
local Base = require(System.Base)
-- then folders
local Assets = ReplicatedStorage.Assets
local Classes = ReplicatedStorage.Classes
-- then variables
local hardcoreModeEnabled = false
-- then rest of the code

Module:

local module = {} -- we make our module's table

-- services
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
-- other modules if needed
local DayAndNightCycle = require(script.Parent.DayAndNightCycle)
-- variables
local DayDuration = 20
local NightDuration = 10
-- then rest of the code

-- and finally return statement
return module 

4. Naming and code structure
Now naming of things, list below:

A) Capital letter after each word in name:

local workingMachinesInBase = 4

B) Important stuff starts with Capital Letter, other stuff not

-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- variables
local notImportantStuff = game.Workspace.Part

C) Tables index starts with capital

local array = {
["Orange"] = 5
}

D) Table’s index is always in [“”]

local array = {
   ["Orange"] = 4,
   ["Red"] = 3,
   ["Blue"] = 2
}

E) Functions starts with Capital

local function EatOrange()
-- code
end

F) Press Tab before each end statement

if Orange == 3 then
   if Orange - Apples < Storage then
      Storage:Explode()
   end
end

G) Use comments to describe most important stuff

local OrangeTable = {} -- very important table, it's the most important stuff in this game!

5. Other rules

A) Capital letter at the start of object’s name (if it’s called by script, no matter how important it is)
B) Always place scripts in System folder
C) Use OOP
D) Use lightweight modules to store unique effects
E) Replace statements with matching using tables

Conclusion
This is it, this is idea how you can write your code and overall make stuff with scripts, i believe you’ll find your favorite or maybe even your own coding style, everything is up to you, good luck with coding !

2 Likes

workspace (all lower case) is defined by default you don’t need to create a new “Workspace” variable

4 Likes

oh, i didn’t knew it, thx for that

2 Likes