The best way to start off is by practicing a clean code-base that you believe would be readable from the perspective of another programmer who has no idea what your code is trying to achieve.
There are several factors behind what makes a code “clean”, one of the more important factors is efficiency—the question of whether your block of code can achieve its intended result efficiently and quick without an unreasonable amount of resources. Another is format and syntax, like indentation & spacing, or consistent naming conventions, which just makes it easier to read each variable and function. The most common programming naming convention used on Roblox is the Pascal Case, but I personally prefer the Camel Case due to my history with programming languages for Java for example.
The names of variables should especially have meaning. One mistake I see here and there is that people just name their variables anything—sometimes it’d just be one letter, sometimes it’d have no correlation with what the variable is supposed to store. For example:
local function roll(a)
local b = 1;
return Random.new(os.time()):NextInteger(b, a);
end
A technical, intermediate or experienced programmer would definitely have an idea of what the variables a
and b
connotate, but that’s because this is just a pretty simple example and the function isn’t too complex that it doesn’t take much to piece two and two together. But for more complex functions, it’ll be especially recommended that your variables have meaning.
Fixed code with updated variable names
Giving the variables actual meanings in their names just makes it easier to comprehend the purpose of the function and also helps you find that one part of the code in case you need to go back.
local function rollDice(max)
local min = 1;
return Random.new(os.time()):NextInteger(min, max); -- return a random value between 1 and the provided maximum
end
Additionally, comments help, too. They’re there for a reason, and they exist—whether to serve as a reminder, or to add credits to a part of a code, or to let another programmer or even future you know what this part of the code does; all of that will definitely help with project organization as you asked. When making functions, you should also focus on allowing a reusability factor for them.
Single-Responsibility-Principle
I figured I should add this as well considering you included OOP (Object-Oriented Programming) within your inquiry. The single-responsiblity-principle (SRP) is a core concept within OOP, and it should be something that you always keep in mind when creating new modules & especially functions. A function or object method for example should only have one, well-defined responsibility or purpose. You shouldn’t merge several functions into one, what if you end up having to re-use them later? Then it’d be inconvenient and also an unnecessary addition of lines to the code by re-pasting or re-typing them.
This also partly applies to modules; if you’ve had experience with other programming languages before, such as Java, then in Object-Oriented Programming, you could think of module scripts as classes. And the principle dictates that each class should have only one responsibility or purpose. This means that, for example, if you have a module script that handles the serialisation of certain values, that shouldn’t be handling data saving as well. Have a separate module script that handles the data.