mnn
mnn

Reputation:

Speed of boolean expression (C#)

Hello I was thinking of what is better to write (in matter of speed and/or efficiency):

bool Method(...) { ... }

...

bool result = Method(...);

if (result == false)
{ ... }
// or

if (!result)
{ ... }

Or, alternatively...

if (result == true) 
// or

if (result)

I'm asking because I use first one (result == false) but sometimes it gets very long, especially in condition ? expr : expr statements.

Upvotes: 2

Views: 1332

Answers (7)

Randolpho
Randolpho

Reputation: 56391

Although I agree with @Noldorin that if(!result) is to be preferred, I find that if(result == false) and its ilk are very useful if you have to test a nullable bool, which most frequently happens in data access scenarios.

Edit: Here's a sample program that explains the different ways you can use the equality operator on a nullable bool.

class Program
{
  static void Main(string[] args)
  {
    TestNullBool(true);
    TestNullBool(false);
    TestNullBool(null);
    Console.ReadKey();
  }

  private static void TestNullBool(bool? result)
  {
    if (result == null)
    {
      Console.WriteLine("Result is null");
    }
    if (result == false)
    {
      Console.WriteLine("Result is false");
    }
    if (result == true)
    {
      Console.WriteLine("Result is true");
    }
  }
}
/* Output:
Result is true
Result is false
Result is null
*/

Upvotes: 1

nes1983
nes1983

Reputation: 15756

I think there are three steps in this process. First, you believe that there should always be a comparison inside an if, so you write if(this.isMonkey == true) banana.eat();

Or, more realistically

if(SpeciesSupervisor.getInstance().findsSimilarTo(Monkey.class, 2) == true) {
    String f = new PropertyBundle("bananarepo").getField("banana store");
    EntitiyManager.find(Banana.class,f).getBananas().get(1).eat();
}

Then, you learn that it is fine to ask if(this.isMonkey) and that this formatting allows better reading as a sentence in this example ("if this is a monkey").

But at last, you get old and you learn that if(b) is not very readable, and that if(b==true) gives your poor brain some clue what is happening here, and that all these harsh claims of "misuse", "abuse", yada yada, are all a little overstated.

And as for the performance. In Java it would not make a shred of difference. I don't think .NET is so much worse. This is the easiest optimization a compiler could do, I would bet some money that the performance is the same.

Cheers,

Niko

Upvotes: 1

Guffa
Guffa

Reputation: 700232

You should definitely use the expression with the ! operator, not because it's faster but because it's safer.

If you accidentally use one equals sign instead of two, you assign the value to the variable instead of comparing the values:

if (result = false) {

For other data types the compiler can catch this, as an expression like (id = 42) has an integer value so it can't be used in the if statement, but an expression like (result = false) has a boolean value so the compiler has to accept it.

(An old C trick is to put the literal first so that it can't be an assignment, but that is less readable so the ! operator is a better alternative.)

Upvotes: 1

Michael Piendl
Michael Piendl

Reputation: 2884

There is no performance difference in runtime code. Most of the coding-guidelines in the companies i worked prefer !result.

Upvotes: 2

arul
arul

Reputation: 14084

Runtime speed is the same - both snippets compile to the same MSIL code representation.

Using (result == false) instead of (!result) feels kinda sloppy though.

Upvotes: 4

Noldorin
Noldorin

Reputation: 147260

Personally, I cringe whenever I see something like result == false. It's a rather nasty misuse of the equality operator in my opinion, and totally unnecessary. While I'd imagine the compiler should turn the two expressions into the same byte code, you definitely want to be using !result. Indeed, it is not only the more direct and logical expression, but as you mention, makes the code a good deal shorter and more readable. I think the vast majority of C# coders would agree with me on this point.

Upvotes: 9

Steve
Steve

Reputation: 12004

I don't think there is any difference, and if there is you would probably have a hard time measuring it. Any difference is likely to be in the noise of the measurement.

Upvotes: 1

Related Questions