Enums are so good. You see:
- They provide type safety.
- They provide code readability.
- They are so useful.
Let us look at this dummy code for instance:
See enums are type-safe, you can not assign anything else other than
predefined Enum constants to an Enum variable. It is compiler error to assign
something else.
It’s so clean. I know what letsRock
wants. It can only take POP
, ROCK
or
JAZZ
. It’s like this piece of code is talking to me (readability).
Now look at this ugly piece of shit:
Who knows what on earth this letsRock
is looking for. I might very well pass 0
if I don’t know about your constants to play POP. Tomorrow some nucklehead will
come and change POP = 10001
and all hell will break loose on my code.
True that.
But as it turns out - Enums are not so good. Actually Enums are bad, especially in mobile environment where resources are limited. Enums consume twice as much memory as static constants. They bloat your app’s size (and reports have been found that they also eat your dog’s food from time to time). Well that doesn’t sound good, right?
The question now becomes how to keep enum like behavior while avoiding enums. In other words, just like enums how do you ensure that the client can only supply these three music types to our interface - nothing more, nothing less.
TypeDef Annotations:
You’d know about typedefs if you are coming from a C background. In android we
have a support annotation IntDef
for something similar.
Basically the @IntDef annotation lets you create a “typedef” i.e. you can create another annotation which represents the type of music. This annotation ensures that only the valid integer constants that you expect are used.
So we decorate our API with a typedef annotation like this:
Now we can use this annotation as follows:
Now if the client tries to enter some int other than these three contants, he will receive a lint error.
Android source itself uses it all over the places:
There is also a string version of @IntDef, aptly named as @StringDef. Checkout the links below to know more.
To know more:
- See this Android performance patterns talk to know more.
- Checkout the documentation for support annotations