Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean vs. Enum for typing "New or Used"

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..

Update

If you prefer enum, please comment on what should be appropriate name for type and property (e.g public NewUsedType NewOrUsed {get; set;})

like image 665
zam6ak Avatar asked Jun 13 '11 19:06

zam6ak


People also ask

Why is enum faster?

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.

Can an enum be of type bool?

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.

Can you iterate through an enum in C?

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.

Can an enum have one value?

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.


7 Answers

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.

like image 150
jason Avatar answered Oct 08 '22 04:10

jason


Use an enum. I would call it Condition. You don't need a None value. You can just make it nullable.

like image 30
Daniel A. White Avatar answered Oct 08 '22 03:10

Daniel A. White


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; }
}
like image 28
Davy8 Avatar answered Oct 08 '22 03:10

Davy8


For New I would use a boolean, an enum in this case would be overkill.

like image 25
Otávio Décio Avatar answered Oct 08 '22 03:10

Otávio Décio


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.

like image 27
FrustratedWithFormsDesigner Avatar answered Oct 08 '22 04:10

FrustratedWithFormsDesigner


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".

like image 26
Tim Avatar answered Oct 08 '22 04:10

Tim


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 Conditions 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.

like image 38
CodesInChaos Avatar answered Oct 08 '22 03:10

CodesInChaos