1

Today I had a rather heathed discussion with a java backend developer. I do the frontend side with jsangular. His rest service sends me an object that includes an enum. I get this as a string and need to show it in an angular grid.

The enums are very self explainable.

Examples what I get: Person = {gender: "MAN", position: "IS_EMPLOYED"}

In the view I have to show the following: man is employed

So i solved it in a simple and generic way: Person.gender.toLowerCase () + ' ' + Person.position.toLowerCase ().replace ('_', ' ')

He found this terrible programming and demanded that I would make a mapper with if else or switch So: If(Person.gender==="MAN") return "man" Else if....

The label name is always the same as the enum name (only lower case and space instead of underscore)

He went on a rampage that it was bad coding and basically terrible and a sin against god...

So my question is this bad/good practice (so a b/w story) or rather acceptable and defendable in some cases?

Typed on my phone so I couldn't put the code examples in the right tags..

1
  • ya your colleague was right. sorry . Commented Feb 26, 2020 at 1:46

4 Answers 4

5

There are a few ways to look at this:

Mapping is more robust: He is right in saying that there should be a complete mapping function. This allows you to handle errors (i.e. if the response you get for position is "banana", you should probably not just throw that out there). This is especially important in backend development when you are probably working with a strict schema.

Scale Matters: There is nothing technically wrong with your approach. If there are a small number of entries or if the user base or application scope is small, it may not matter much. It doesn't really conform to best practices, but it works, and if that is all that matters, it might be enough. Although you should always strive for best practices, you need to keep the big picture in mind.

Efficiency: One approach may be more efficient than the other. The difference may be negligible. In web dev, speed is a priority, and if both approaches get the same result but one is much faster, choose that one.

Sign up to request clarification or add additional context in comments.

Comments

2

It'd probably be a better idea to make a client-enum map - then just send back the actual value. So you could have:

var personTypes: {
    "1": "Is Employed",
    ..
}

Then simply send 1 back as the enum and use the lookup object:

console.log("Person status: " + personTypes[Person.position]);

Which also makes it easier to display a list of types - pick a new one, and simply send the enum back:

<select ng-model="personTypeEnum" ng-options="enum as type for (enum, type) in personTypes"></select>

5 Comments

But he sends the enum name. And I really do not see the point in making a mapper var PersonTypes: { "IS_EMPLOYED": "is employed"} that just seems a terrible waste of typing an just look silly
@brokenhip -- The mapper is handy if you have to send data back and use the enum. If you never have to, then just make a mapper for the enum string to the friendly string. You can either do that or a bunch of if else statements.
I have that as a filter that i use in nggrid with cellFilter so the original string isn't changed when sending back. His problem is that i use the enum itself to make the label instead of having a bunch of if else statements. But is that wrong?
He even threatened to make non sensable enums so I would ne forced to write label for each. It just doesn't make sense to me and would like to get a sensible explanation why how i did it is so wrong
It's not the best idea. I'd just use the mapper.
0

Check in your console with printing object. example if object is person then if you are rendering this object as json, gender(enum) would be as

{"success":true,"person":[{"class":"person","id":26,"comment":null,"gender":{"enumType":"com.project.Person$Gender","name":"MEN"},

so u can print person.gender.name, which will give MEN

Comments

0

The approach in the question make it harder to internationalize the application. I would also prefer a central utility function to map enum values to display values. If you ever need to internationalize the frontend, you would simply return appropriate display values depending on the current language.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.