r/CodingHelp • u/LeoPixel11 • 12d ago
[Other Code] Class instance while running the code
I started actually writing code with OOP yesterday (using Dart... In case it is necessary to mention) and there's a thing that kept bothering me as I scrolled through their page and watched the tutorial. Everytime they gave a class example, they created an instance themselves and never based on the code running.
Imagine a system to simulate a store's stock. There'll be items going in and out, that shouldn't be inserted by the programmer, so there must be a way to do that "dynamically".
I'm aware that this might be me simply missing something from OOP classes, but I want to see what other coders have to say about that.
Any help would be welcome. Thank You
1
Upvotes
2
u/rupertavery64 11d ago
There are a bunch of things to unpack here. First one is data modelling.
Typically, a store database would have items. Each item would be assigned a unique id, and a name, and the quantity. So, they are all just one thing - an "item".
You don't need different objects for each type of item. At least, for the purpose of keeping track of inventory.
An item might have a description, and maybe a list of associated properties: type, weight, volume, color, and so on, depending on each item. Again, you don't have to create a new class for each thing. It could just be an associative attribute.
But what you are probably talking about is either generics or reflection.
In some languages, you can inspect the type of an object at runtime or use language constructs that check the type and do something different with it.
Of course it depends on your data, your data model and the language you are using.