How important is it really to include things like encapsulation and inheritance in Roblox OOP?

Currently I am going to school for software engineering, and I am told a lot that the most important part is to do coding outside of class. So lately I’ve been trying to code but make it look good for employers if I were to have a portfolio. However, I am running into a lot of problems with coding with OOP in Roblox. It might be because I’m just not good enough at coding yet, but I want to know all of yours opinion on the importance of these elements in a object oriented program.

1 Like

To answer your question, it really just depends on what you’re doing. 99% of the time, I don’t incorporate any sort of inheritance when I’m dealing with OOP


Specifically to Roblox coding

1 Like

Is this specifically in the case of Roblox coding, because I know the options are limited. Or do you mean just in general.

1 Like

Encapsulation depends on personal preference because features like private fields or methods don’t exist in Lua, so it’s up to the programmer to decide how they want to set it up (if at all). For example, I’ve seen some people using local functions as private methods or naming private variables differently to distinguish them from public ones, while others don’t bother.

Inheritance is very useful, but overusing it can lead to some horrific organizational problems. My principle is to only use single inheritance and limit inheritance chains to 3 classes (parent → intermediate → child). A lot of nested __index calls can hurt performance (I don’t know exactly how bad this is), so you don’t want to overdo it.

If you want to implement some form of multiple inheritance (a child class inherits from parent A and parent B), use composition instead (the child class contains objects of parent A and parent B).

2 Likes

Ok, thank you for all the responses, I really appreciate it. The main reason I ask is because my professor has been drilling into us to try our best to use good practices like organizing code, naming variables and commenting on everything. Most of which makes sense except when it comes to roblox code, where they dont have public and private methods of a class. I have been doing some researching and found some wrap arounds on youtube videos, but It just runs into even more problems as I go and seems to be doing more harm then good.

1 Like

Have your professor touched on typehinting/type annotations, which is a prevalent feature in many mainstream languages? It exists in Luau as an optional, gradual typing system. You can use it for OOP if that helps, but in an unconventional way.

Because Lua itself already unconventionally supports OOP via all those metatable thingamajig, you should do everything you can to make it as comfortable as possible, and annotating its types is one such thing you can do. It has almost* no effect on code compilation**; it’s most useful to the IDE’s linter and autocomplete feature.

* Typecasting can affect semantics

** Typing doesn't affect compilation to bytecode, but CAN affect compilation to assembly via Native Luau