Introduction
Hello! Lately I’ve been noticing that everyone has a different formatting technique. But I’ve noticed that some are hands down just better. I really wish people would use these techniques, because when your code is organized, it’s easier to write, debug, and look at. Today I will be giving a few tips on how to write organized, formatted code.
1. Variables / Identifiers
An identifier in programming is the name you give to a variable; to identify it. These can be very important because it’s what you use to tell the computer what to do. They can also be important to you, because it’s what you use to know what you’re doing. Have you ever wrote a program 5 years ago, and went back on it, and didn’t understand what you wrote? Even with comments! Here are some tips for formatting.
local player = game:GetService("Players").LocalPlayer
local p = game:GetService("Players").LocalPlayer
Which one do you think is better? The first one! We can clearly understand what it is. The player. We don’t have to scroll back up to figure out what it was last assigned to.
Take another example:
for _, v in folder:GetChildren() do
print(v)
end
I’m sure EVERYONE does this. Even I do. And you may not realize it, but it’s actually bad practice. Because what is v? I have no idea! Are we looping through parts? Scripts? Or even the services? I don’t know! Use specific names when dealing with identifiers!
Formatting
Now this one may be subjective, but here’s one thing that isn’t. INDENTATION. Use it!
Imagine someone asking you to fix up a script, and you get this:
local part = script.Parent
local wall = game.Workspace.Wall
local canOpen = true
local function lift(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid and canOpen then
local player = game.Players:FindFirstChild(partParent.Name)
local gold = player.leaderstats.Gold.Value
if player and gold > 100 then
canOpen = false
player.leaderstats.Gold.Value = player.leaderstats.Gold.Value - 100
wall.Transparency = 1
wall.CanCollide = false
wait(2)
wall.Transparency = 0
wall.CanCollide = true
canOpen = true
end
end
end
part.Touched:Connect(lift)
What in… the world! How am I supposed to read this! I don’t know either! It’s because people don’t right their code in that way, so they won’t understand it. Code is structured into blocks, and that’s how people and computers interpret it. So you need to format it that way, also. Here’s a better way to write it.
local part = script.Parent
local wall = game.Workspace.Wall
local canOpen = true
local function lift(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid and canOpen then
local player = game.Players:FindFirstChild(partParent.Name)
local gold = player.leaderstats.Gold.Value
if player and gold > 100 then
canOpen = false
player.leaderstats.Gold.Value = player.leaderstats.Gold.Value - 100
wall.Transparency = 1
wall.CanCollide = false
wait(2)
wall.Transparency = 0
wall.CanCollide = true
canOpen = true
end
end
end
part.Touched:Connect(lift)
The rest is just preference. Try not to use too many spaces, or too little spaces. Period.
Use comments!
Even if you don’t have good formatted code, or can’t seem to figure out how to get it that way, explain it! Explain your code detailed and well, so that we can all understand what it’s doing. That’s the whole point, anyways.