Difference between using OOP and ModuleScripts

As title says, what is difference between those two?

Both does the same job (being able to be used multiple times)

2 Likes

this post may help u

1 Like

Both are ways of achieving code reuse and preventing spaghetti.

In Lua, modules are the highest tier of scope-based code organization. It’s like a code block where you can define local variables that can’t be accessed directly by the user, only indirectly through an interface that you as the programmer control. In that way modules (can) achieve encapsulation. Since they can be required from any number of other pieces of code, they enable code reuse. If you write lots of services with a limited set of responsibilities, you can reduce code coupling. However all of this can be achieved with a code block, e.g.:

local myLibrary
do
    local secret = 1234
    local password = "please"
    myLibrary = {}
    myLibrary.tellMe = function(_password)
        if _password == password then
            return secret
        else
            return "ah ah ah you didn't say the magic word"
        end
    end
end

The purpose of OOP is to allow you to do object oriented analysis and design. It’s a specific way of doing abstraction, which feels natural to many people because we often think in an object-oriented way due to how many natural languages work, with nouns, verbs, subjects, adjectives etc. It’s also an area with lots of knowledge freely available because of it’s popularity, so it’s kind of the “default” paradigm. The advantages of OOP that are commonly taught are encapsulation (private variables), inheritance (classes and sub-classes) and polymorphism (you should look up examples xP). Inheritance and polymorphism are two additional ways of achieving code reuse.

Different OOP languages have different ways of implementing those 3 things, with different advantages and disadvantages. Lua is not OO, so you need to implement those 3 things yourself. Well, actually polymorphism kinda comes for free in Lua since it’s weakly typed. Encapsulation is usually achieved with modules and private fields (like I showed before), while inheritance is usually achieved with metamethods.

There are several libraries out there that let you define OOP classes if you want to try and get a taste for it before diving deep into metamethods.

And keep in mind that you don’t need OOP to write good code, and it doesn’t really help you to write good code unless you understand what OOP is even trying to help you with and how to actually make that happen (those 3 things mentioned above).

1 Like

So from what I understand in your reply is that there is nearly no difference?

They have similar benefits, but no they’re completely different in pretty much any other regard. You can use OOP without modules and you can use modules without OOP, or you can use the two together.

1 Like

A ModuleScript is a language tool. It allows to split and reuse code. Without ModuleScripts, we would have to write very long and difficult to read scripts.

OOP is a programming style (or programming paradigm), which is a way of thinking about a problem and organizing code. When we use OOP to solve a problem, we must imagine that everything in the problem are objects that interact with each other, do things and share information to solve the problem (similar to a company where workers work together to solve a problem).

Are very different things. With ModuleScripts you can divide and organize the code, but there are no rules about how to organize it. With OOP there are strict rules for how code should be organized. OOP requires the language to have mechanisms to split and organize the code (in luau, functions or ModuleScripts). ModuleScripts are just another tool of the language (luau).

just a few comments:

OOP (object oriented programming) is the implementation of OOD (object oriented design) which is in turn the concrete form of OOA (object oriented analysis).

In reality the abstraction is mainly in inheritance. Similar to how we organize nature (animal kingdom, plant kingdom, mineral kingdom, …) and easy to understand for people. that of “… nouns, verbs, subjects, adjectives, etc” sounds more like DOP (data oriented programming).

OOP is a pretty complicated world.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.