I’ve come to realize in my time learning Roblox development that many people do not actually organize their code much and just go line by line. I however feel much nicer when I code to have a strict template that I follow. Every single one of my scripts I ever write has this template and I’ll explain why!
The template:
--[[SERVICES]]--
--[[FOLDERS]]--
--[[MODULES]]--
--[[TYPES]]--
--[[VARIABLES]]--
--[[FUNCTIONS]]--
--Function Declarations
--Function Definitions
--[[SCRIPT]]--
Example of a Filled out template:
--[[SERVICES]]--
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
--[[FOLDERS]]--
local SS_Modules = ServerStorage.Modules
--[[MODULES]]--
local DataHandlerModule = require(SS_Modules.DataHandler)
--[[TYPES]]--
type PlayerProfileType = DataHandlerModule.PlayerProfileType
--[[VARIABLES]]--
local PlayerProfileList: {[Player]: PlayerProfileType} = {}
--[[FUNCTIONS]]--
--Function Declarations
local PlayerAdded: (Player: Player) -> ()
local PlayerRemoving: (Player: Player) -> ()
--Function Definitions
PlayerAdded = function(Player: Player)
local PlayerProfile: PlayerProfileType = DataHandlerModule.GetPlayerProfile(Player)
PlayerProfileList[Player] = PlayerProfile
end
PlayerRemoving = function(Player: Player)
PlayerProfileList[Player] = nil
end
--[[SCRIPT]]--
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
The order in which the template is made is based off of what you would need when filling in code for each section. For example, in order to write your functions, you need your variables, sometimes you also want to have specific types for those variables. Those types can be from module scripts, so module scripts have to come before. In order to reference the module scripts you need to be able to reference where they are, aka their folder. Then finally in order to reference the folder you need to reference the Service.
I realize this may seem unnecessary to some of you as most people generally code like this just without actually explicitly writing it out, but maybe having the explicit Template will help you like it helps me.
For people curious as to why I have Functions Declared and Defined is because I like to alphabetize my functions for my own readability so I have an easier time navigating. Although you can probably assume that alphabetizing makes functions hard if you call functions that are alphabetically before the one in use, therefore declare all of them beforehand and you are good to go.
Let me know what you guys think and what is your personal tips and tricks you do to keep your code Clean and Readable.
I will also add that I am a “Never Nester” and love type checking wherever and whenever possible.