"Enum" should be used to indicate business rule options?

Asked

Viewed 140 times

2

Let me give you an example that is better: in the supermarket there are several categories of products such as vegetables, canned vegetables and others. To register more enumDo I have to change the code or do I have to change it dynamically? If I register only three in my app I will have to change the code to add more later?

4 answers

6


See the difference between something being constant and something having the forbidden modification: What is the difference between const and readonly?. It’s C# but it’s just to understand the concept.

A enum is just a collection of constant. Not data that should or can be changed. It’s good for enforcement mechanisms, not for business rules that are usually mutable. I notice a certain abuse of enumerations to do this. There’s a question about this.

So it’s not even a question of having to change the code, it’s the wrong mechanism, which generates consequences.

Business rules tend to be better in data collections designed to be expanded or modified. Therefore the category should be an object like any other and should have a list of them that can be easily changed as needed.

I would use, if I understood correctly, a list or other structure that suits the question well with Categorias in it, so it has a business object and not an application mechanism.

2

The Tipos Enum are not meant to be dynamically added/changed/removed. Quote documentation of Java itself:

You should use Enum types any time you need to represent a Fixed set of constants

In other words, you should use enum to represent an intended/fixed/invariable set of constants.

To represent some information that are immutable, as the days of the week (Monday, Tuesday, etc.) enumerators are very useful. After all, what is the possibility of adding a new day of the week to our calendar? :)

In your case, you need to choose to keep these categories dynamically, out of the compiled code of your project. That is, out of a enum. Usually this is done using a database that, in your case, could have a table categoria to be able to register the supermarket product categories and read these categories from a query to this table in the database.

  • Thank you very much. The database is ready I had doubts about using Enum. I’m glad it won’t be hard to replace rsrsrsrsrsr.

1

I think not, because the enumerator is a type that can treat a set of defined values, so I think using the enumerator dynamically may not make sense, in the case that you demonstrated the right one would be to use the product object, with product name as attribute, then the list of products could grow dynamically.

  • I think it’s best to forget the In this case because there will always appear a different type of product to register.

  • Yes, so using a Product class would be better.

  • 1

    I was thinking of putting the product category as Enum, but there are so many categories srsrsrsrsrs

  • Yes, I understand that there are situations like this kk. When it is dynamic try to do as I told you I think it will help you more.

1

Imagine that the Enum fits very well to sizes of coffee cups from a coffee shop, in it there are only cups with sizes P, M and G. In this case you do not want a user register a cup that he will not be able to sell, an XL for example. As the friend answered above, set of well-defined constants.

Another example: in an application for a farm, the animals are separated by sex, in which case there cannot be an animal that has a sex other than male or female.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.