June 15, 2008

Enums, Objects and the joys of being typesafe

Filed under: as2, as3, education, flex, tip — michaelangela @ 1:51 pm

I like autocomplete. I also like it when the compiler (or IDE) tells me I am doing something wrong BEFORE I compile. Flexbuilder is my AS3 compiler of choice. Although FlashDevelop 3 has come a really long way. It’s PC only though. 😦

Anyway, just did some reading about Enums in AS2. This bit of reading below came courtesy of a link here about creating typesafe enums in AS3. I really like this. I particularly like the idea of the “Month” class given in the comments on the page below. In addition to being typesafe, by attaching an ordinal to each value, you can compare your values. So in that case Month.JAN would be less than Month.MARCH. (There is also a discussion about “SCREAMING_CAPS” that was enlightening. I digress…)

blog-j: Symbolizing, Coding, Objectifying, Processing » Code. Code. Code.

I’m in the middle of reading Code Complete, Second Edition by Steve McConnell
(a book which I can’t recommend highly enough). One of the key points
of the book is that just because your language doesn’t natively support
a certain implementation ideal (for example, private field members or
classes), you should work around those limitations by ‘programming into the language, not in the language.’ One example he gives of this is Enumerated types.

The definition of an Enumerated type is this: “A list of named
values used as the range of a particular attribute type. For example,
Color = {Red, Green, Blue} (source).”

This can be useful in limiting possible values for a methods
arguments (and more). For example, by using an Enumeration of
SearchTypes, you can ensure that the searchType argument falls into the
valid values for a SearchType.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: