This question has in the back of my mind for some time, sorry if it appears subjective. There are some disadvantages in using bool in public properties and constructors for data objects. Consider the following code as an example.
Using bool:
public class Room
{
public string Name { get; set; }
public bool Bookable { get; set; }
public Room(string name, bool bookable);
}
and the use of this class
Room r = new Room ("101", true);
This is suitably functional, there is however another way to implement it:
Using enum:
public enum BookingStatus
{
Bookable,
NotBookable
}
public class Room
{
public string Name { get; set; }
public BookingStatus Bookable { get; set; }
public Room(string name, BookingStatus bookable);
}
and the use of this class
Room r = new Room ("101", BookingStatus.Bookable);
To me the two appear functionally equivalent, there are some advantages and disadvantages of each however:
- When setting properties the Enum method is more verbose (you can infer the usage of the enum from the code alone)
- Enumerations can be extended to support further states (particularly useful for an API)
- Enumerations require considerably more typing (although reduces this vastly)
- Enumerations can not be used in conditionals (i.e. if (r.bookable)), although I appreciate this is trivial to resolve.
Am I missing something, totally off the mark? I am not sure why this bugs me so much, perhaps I am too OCD for my own good!