Our Sonar code analysis server tells me
Boolean literals should not be redundant. Redundant Boolean literals should be removed from expressions to improve readability. Tag: Clumsy
and categorizes it as a minor bug.
Is this an error in the associated Sonar/FXCop C# rule (maybe it cannot handle nullable types?!) or is this really clumsy?
Note: createMissing is a value which comes from a datastore (Edit: in this implementation it is parsed from a XML document) where null is a valid value (because the attribute is not mandatory) and in my business logic null means equals false).
[XmlIgnore]
private bool? createMissing = null;
[XmlAttribute("createMissing")]
public bool CreateMissing
{
get
{
return createMissing.HasValue ? createMissing.Value : false;
}
set
{
createMissing = value;
}
}
public bool ShouldSerializeCreateMissing()
{
return createMissing.HasValue;
}
Nullable<T>has a method calledGetValueOrDefaultwhich is the equivalent of your code asfalseisdefault(bool). \$\endgroup\$