i am building a small app that act like a digital camera and i am trying to understand if this is a good oop way, after i build a camera object i am "giving" it a memory card class, the memory class get the type of card from enum class and the size form a enum class,

now i need to incorporate those two into an array that is the memory card.

5 Answers
5

So for example if you will never need to worry about a capacity other than 2, 4 or 8, then there's no problem. If one day you will have a 16GB card, then you have an issue. You either

change the code. This may of course be trivial depending on the complexity of the application and the release procedure. Or it may be horrendously complicated, only you would know this. Or,

think of these things as a list of static data. Possibly read from a
database? But basically a mechanism that will allow you to expand the list
over time without having to rebuild your application. If you adopt this approach, then possibly enums are not
the way to go. A capacity/description pair perhaps?

Obviously without knowing more about your app its not really possible to go much farther but I would encourage you to think whether enums really are appropriate.

There's nothing wrong with your approach, but it really depends on how you plan to use the class.

What you should be wary of, however, is code that inspects an object (in this case, the type/size of the memory card) of your class to work out how to use the object. As far as possible, it should be up to the object to decide what to do with its state, not external/client code.

This may seem vague, but without more detail it's difficult to give any concrete advice.

However, is there a reason you've chosen to use the size in megabytes as the values for your enums? It's not at all obvious to someone using your code that this is the case.

Personally, I would not use enums if the values really mean something pretty important. You cannot really ensure that the values are what you expect. Especially, if there are other developers working on the same project.