Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. so, let's say you're on a team and you're working on a an object that stores information about a car
  2. let's write some pseudocode
  3. class car {
  4. Frames frameType;
  5. TireSizes tireSize;
  6. }
  7.  
  8. Now, the way you used to write stuff like this, like in C, or strongly typed OOP languages pre-enum, or when i was a babby programmer:
  9. as a global:
  10. FRAME_SEDAN = 1
  11. FRAME_TRUCK = 2
  12. ...
  13.  
  14. or
  15. class Frames {
  16. const static SEDAN = 1;
  17. const static TRUCK = 2;
  18. private Frames() {} // uninstantiable!!
  19. }
  20.  
  21. this is a pretty common pattern in programming, so "enums" were made to make the pattern simpler, and along with it comes some neat optimizations
  22.  
  23. enum Frames { Sedan = 1, Truck = 2 }
  24.  
  25. Let's say we don't care about the "value" in memory, either. we just want to be able to work with words instead of numbers and let the compiler/computer figure it out, we can do this in most languages!
  26.  
  27. enum Frames { Sedan, Truck }
  28.  
  29. And the compiler will create values for them.
  30. At compile time, the enum is never stored! the values are replaced as the program is compiled.
  31. AND having Frames be an enum tells your co-workers that Frames is not an instantiable object and is only used for enumerating the various values. They don't even have to look at the source. They can see it in code completion in their IDE!
  32. It's a small thing, but it's useful :^)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement