111: Data Types: Enums.
Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorías:
What would you rather see? 1, 2, 3, or red, green, blue? Enums allow you to give meaningful names to values. Seems simple, right? It is, except for a few things you should be aware of. This episode explains the following six aspects of enums that you need to be aware of in order to use them properly: Can you declare the underlying storage type that will be used? Some languages allow this and some just use an int. And some like early versions of C++ would try to use an int but could use something else if an int wasn’t large enough. Can you specify the enum values themselves? There are times when the exact values are important. What if you want different names for the same value? Or maybe you need to interface with some other code that uses specific values and you need to make sure that your code matches. What scope do the enum values occupy? This is important to understand so you know how to handle name conflicts if any. Early version of C++ would promote the enum values to the scope that the enum itself is declared in. Actually, C++ still does this but added a new enum class type that keeps the enum values scoped inside the enum. Will enum values convert to integers automatically? This is critical to understanding how you can use enums. If you ever want to combine enum values or save them to another type such as an int, then what will the compiler do? Can a variable of the enum type hold values not declared in the enum? If you have an enum that defines only red, green, and blue with the values of 1, 2, and 3, then is it possible to have a variable of this type that contains the value 5? You’d have to perform a cast to get the value 5 into the enum but most languages will allow this. You should never assume that just because you’re using an enum that you will know the values it will contain. It’s still wise to check ranges and have default switch statements in place to catch unexpected values. Can the enum type be used for flags? The older C++ enum type could support this because of the way it allowed conversion to ints automatically. Because the newer enum class type doesn’t work like this, then flags will be better implemented with another method. Consider using the bitset class. For C#, you can add an attribute called Flags that will let the compiler know you intend to use the enumeration to hold flags. Listen to the full episode or you can read the full transcript below. Transcript The first thing to understand is that different languages usually have some concept of declaring enumerations or enums for short but there are differences. Before we begin, what’s so bad about just using numbers? Well, the first thing is that you’ll probably want to use specific numbers consistently throughout your application. It would be very bad if part of your code thinks 1 means run very fast while another part thinks 1 means to stop and hide. You can help solve this by declaring constant ints for each of the various meanings you want to represent. Then use those constant ints instead of the values directly. This will at least make sure that your hero runs consistently. But the values are still ints. Just because you’re using some specially named values isn’t enough to enlist the compiler’s help to make sure that you don’t accidentally assign a constant describing a type of shrubbery to your hero’s movement. Since everything’s an int, the compiler can’t help. The biggest advantage you get with an enum then is the addition of a new type. You can group values together that make sense together. Enums allow you to move away from sprinkling raw numbers throughout your code, allow you to refer to these raw numbers with meaningful names instead, and allow you to group these names into their own type. They’re definitely the sort of thing that can make your code easier to read and more reliable. There’s some details to be aware of though that I’ll