dimlah / izassign

0 stars 0 forks source link

iz: Enum? #1

Open puj opened 9 years ago

puj commented 9 years ago

Hey @dimlah !

Could you tell us a little bit about why you chose this structure? https://github.com/dimlah/izassign/blob/master/app/src/main/java/com/izettle/assignment/event/LayoutChangedEvent.java

What were the alternatives? Why did you choose this way?

Thanks! //Van

dimlah commented 9 years ago

You mean the constants vs enum, or in general the Event structure?

In case of constants vs enum, would be probably better to use enum instead, as it has more advantages than the easiness of using constants. The EventBus structure was just for showing the EventBus use, as I could easily pass the handling of menu to the fragment.

oh I just saw the title of the issue :) but I leave both comments anyway!

Thanks D.

puj commented 9 years ago

What kind of advantages would you see using an enum here? Is there any way which an enum would provide the same ease in this case in relation to the EventBus?

Do you have any questions @vincenthauser?

dimlah commented 9 years ago

Using an enum here would make more obvious what the constant is about so when reading LayoutType.LINEAR is more clear that LayoutChangedEvent.Linear. Also I could have added as value to the enum the actual id of the menu item so I return it without setting layoutType = LINEAR in onOptionsItemSelected.

Also type safety is another common advantage of using an enum instead of constants, making sure that the developer will use the methods, classes etc that need the enum the correct way, with the correct arguments.

Talking about enums, I had a discussion with one of my colleagues the other day and she claimed that android developers blog claimed that we should avoid enums in android. Granted, having an enum though means that you have another class and back in the days there was a debate around this issue. But it has been removed and here http://stackoverflow.com/questions/5143256/why-was-avoid-enums-where-you-only-need-ints-removed-from-androids-performanc/5264861#5264861 there is an explanation from and android VM guy.