I have a message entity say (Message) which can be configured by the creator as
IsWebOnlywhere by it will be shown only on regular websiteIsMobileOnlywhere by it will be shown only on mobile version of the website- The message can also be set to be shown on both forms (regular and mobile) of the website
I want to capture this property as a Nullable boolean. say X
public class Message {
public bool? X {get;set;}
public bool IsMobileMessage { get {return X.HasValue ? !X.Value : true } }
public bool IsWebMessage {get {return X.HasValue ? X.Value : true} }
}
- What would be the most readable name for the property?
- Should this tri-state be captured with a
Enuminstead of anullable boolean?- A
Nullable booleanseemed to be more attractive to me as there will no further additions to the state of the property.
- A
Absolutely, this would be self-documenting and much easier to maintain – go with an enum. Your current code is confusing at best, just looking at it I have a hard time understanding the logic – I wouldn’t look forward to maintain that code – conventionally a nullable bool would be used to capture true, false and the absence of a value as third state. Re-interpreting the value absence (
null) to mean something different is only inviting trouble.