Tips on structuring with OOP

Hello!

Recently, I’ve been learning a lot of cool stuff with OOP but the more I learned the more I wanted to use this knowledge but I’m always unsure if it was really needed.

So I made a list of questions for you to answer.

  1. In which situations is it better to use metamethods opposed to getter/setter functions for detecting when properties are accesed/modified.

  2. Are there really any advantages of using custom datatypes/properties except for value control (restricting to certain types, limits etc), arithmetics and methods?

For instance, currently I have a Setting module that uses getter/setter functions used to control the setting type and other things but I’m unsure if I should create a datatype that stores all the settings of a specific player and which uses metamethod instead of getters and setters to properly control everything.

Thanks!

1 Like

Object oriented programming for a settings module is overkill. You only really need OOP when you need to have stateful objects from a class you’ll find yourself using. Even if you change it to just being a singleton, feels like too much. Very easy to get into the bad habit of overengineering when you first learn OOP and you’re excited to use it somewhere but don’t know where it fits.

Metamethods to simulate change signals and getter/setter pair functions aren’t purpose-interchangeable but they can be merged. I personally prefer being explicit so I fire off my changed signals when using setters unless I have some obscure use case or compatibility issue where it’s somehow more difficult to add a signal than to use a metatable. If you rely on meta to track changes then you also need to access the actual table by proxy so index/newindex is always getting called.

Can’t see any advantages to pseudocustom datatypes or properties. All-in-all, unless it’s a pure Luau system, you will get constrained by the API to use Luau/Roblox types because they obviously can’t accept pseudocustom types. The cases you might need them seem niche so better to think about the actual system you’re trying to create and whether or not you think it’d be appropriate to facilitate the value with a custom type or if there’s a more convenient, smaller way to do it.

3 Likes

Well I’m not sure, there are a lot of functions to check if the values are correct, if the user has access (if from the client) and others meta properties like limits etc. Also in my case, every settings is also stored on the server so I need to manage my players. As I’m not an expert, I’m telling you this to get more accurate results.

1 Like