So, I just hate using true
/false
as method arguments for "enabled"/"disabled". To freely quote Jeff: "I dislike it on a fundamental level".
I repeatedly find myself defining my own enums on every new project in different namespaces all over the place, like these:
public enum Clickability
{
Disabled,
Enabled
}
public enum Editability
{
Disabled,
Enabled
}
public enum Serializability
{
Disabled,
Enabled
}
Is there a generic enum I can use for these scenarios?
The problem with this is that it doesn't actually help in the real problematic cases which is when there are multiple arguments and it is not clear what the 'flag' is controlling.
If you follow the rule that you should 'avoid double negatives' then simple single booleans are fine:
public static void Foo(bool useBaz)
public static void Foo(Ability useBaz)
Then Foo(true)
, verses Foo(Ability.Enabled)
and Foo(false)
, verses Foo(Ability.Disabled)
are really pretty obvious to most.
However when you hit a method like:
public static void Foo(
bool useBaz,
bool barIsHigh,
bool useFlibble,
bool ignoreCase)
then it matters not whether you use booleans or general enums they still both end up looking like this at the call site:
Foo(false,true,false,false);
Foo(Ability.Enabled,Ability.Enabled,Ability.Disabled,Ability.Enabled);
Neither is pretty.
Using specific enumerations for the case in hand:
enum BarOption { Off, On }
enum BazConsidered { Low, High }
enum FlibbleOption { Off, On }
// for case sensitivity use System.StringComparison
then you get
Foo(Bar.On,
BazConsidered.Low,
FlibbleOption.On,
StringComparison.IgnoreCase );
or, if all are simple boolean states and likely to remain so then using a Flagged enumeration better still.
[Flags]
enum FooOptions
{
None = 0,
UseBaz = 1,
BazConsideredHigh = 2,
UseFlibble = 4,
}
Then you would have:
Foo(FooOptions.UseBar | FooOptions.UseFlibble, StringComparison.IgnoreCase);
Appropriate selection of the 'active' flags so that you specify only what is uncommon will then lead to highlighting 'uncommon' usages.
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