I have a perhaps silly question... I am trying to determine what is the best approach for typing a property whose value will be either "New" or "Used". Do I use an enum
or should I just go with a boolean
(e.g. IsNew)? If I go with enum
, what should I call this type and property name? (public NewOrUsed NewOrUsed {get; set;} <-- confusing)
Obviously, with enum
I can have state that declares neither value (NONE, NEW, USED) and enum
is more future-proof (although one can claim there is no chance other values will exist). Again, what is the appropriate name for such a type and property?
With boolean
- it's pretty straight forward (either using IsNew or IsUsed)
I know it's silly, but I am curious what others think
Regards Z..
If you prefer enum
, please comment on what should be appropriate name for type and property (e.g public NewUsedType NewOrUsed {get; set;}
)
Casting from int to an enum is extremely cheap... it'll be faster than a dictionary lookup. Basically it's a no-op, just copying the bits into a location with a different notional type. Parsing a string into an enum value will be somewhat slower.
We can create bool using enum. One enum will be created as bool, then put the false, and true as the element of the enum. The false will be at the first position, so it will hold 0, and true will be at second position, so it will get value 1. Now we can use this as datatype.
you can iterate the elements like: for(int i=Bar; i<=Last; i++) { ... } Note that this exposes the really-just-an-int nature of a C enum. In particular, you can see that a C enum doesn't really provide type safety, as you can use an int in place of an enum value and vice versa.
With an enum type with only one value, most developers will not expect that (UserIDEnum)42 is used, since it's not a "defined" value of the enum.
enum
is more future proof
Not really. You have no idea what the future will bring. If you need to change it later, then change it. So, KISS. Go with:
public bool IsNew { get; set; }
Boom, done. Move on, add value elsewhere.
Use an enum. I would call it Condition
. You don't need a None
value. You can just make it nullable.
You mentioned in a comment "I allready [sic] have a Condition - it refers to a condition when something is Y"Used" (e.g. used vehicle/book can be in "fair", "like new", "mint" condition) ;)"
So with that piece of information, I'd definitely go with a bool
. You already have an extensible way of establishing the condition so this is more of a categorical thing. You may some time in the future you may want something whether more than one condition counts as "New" say:
public enum ConditionType {Fair, LikeNew, Mint, New}
public ConditionType Condition { get; set; }
public bool IsNew
{
get { return Condition == ConditionType.New || Condition == ConditionType.Mint; }
}
For New I would use a boolean, an enum in this case would be overkill.
I would go with the enum, if I found myself constantly translating the boolean value into the value "new" or "used".
If I were more interested in this value as an indicator (such as doing if(something.isNew)
), I'd use a boolean.
eBay has New, and New Other, to distinguish between merchandise sold in its original packaging and merchandise sold in different packaging or no packaging. I'd go for the enum for its extensibility, what you called "future proof".
I'm generally opposed to over-engineering, so my first instinct was using a boolean.
But conceptually it doesn't feel like a logical boolean value. It feels like an enumeration which happens to have two values at the moment. I typically think about the conditions as new
and used
and not as new
and not new
in my head.
I expect other Condition
s being added at a later point in time, such as refurbished
. But of course refactoring from a single property won't be that hard thanks to IDE features like Find All References
. And you'll need to retouch the logic using this property anyways once you add new values to the enumeration.
Future-proofing is the less important reason for me. That it doesn't feel conceptually clean is more important IMO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With