dimanche 3 octobre 2021

Readability with "if (!myBool)" statement in C#. Is there another way?

The perfect way to create an if statement with negation is if (!myBool) { Do(); }. I strive to write my code without smell and make it more readable. But I barely see the exclamation mark, so such code doesn't satisfy me. Although it will not be tolerated, I would like to write if (myBool == false), because unlike others this line is more comfortable for me than previous one.

I have no needs neither in 'if (myBool == true)', nor in if (myBool != true), nor if ((myBool == false) == true), nor another mess. Only just if (myBool == false) to avoid an exclamation mark, but according to SonarLint it's not correct.

I also tried to use multiple !. It doesn't fit as well.

Additionally I tried those things:

if ( !myBool) - issue

if (! myBool) - issue

Is there a way to write more elegantly and preferably avoiding ! and without next?

bool myFalseBool = !bool;

if (myFalseBool) { Do(); }

May it be something like if not (myBool) (this example doesn't possible, though)?

Or maybe?

public const bool NOT = false;

public const bool FALSE = false;

if (NOT == myBool) { Do(); }

if (myBool == FALSE) { Do(); }

What do you think? Have any ideas?

Aucun commentaire:

Enregistrer un commentaire