views:

521

answers:

7

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 variation with true and if(result))

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

+1  A: 

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.

Steve
+8  A: 

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.

Noldorin
Well I write if(result == false) mainly because I find it less readable than if(!result). It's just because I immediatelly see what's being compared.
I can sort of see where you're coming from (many years ago I was inclined to do the same), but in actuality it stems from something of a misunderstanding of how booleans and if statements work. Just start using !result and you'll get used it very quickly - soon you'll be seeing at as *more* obvious!
Noldorin
Yeah, I'm former VB6 developer (about 3 years), and I have just adjusted to C# (much better syntax than VB). Now, sometimes I regret that I started with VB and I think that I never will be "proper" developer when I was affected a lot by VB (even though I always strictly declared variables)
Don't worry. :) I was in exactly the same boat, having started out programming with VB6, then I moved to VB.NET and eventually C#. Now, I am confident of writing clean (nay, even elegant) code, conforming to the guidlines, and generally being aware of "the right way" to do things.
Noldorin
If you find C# syntax more aesthetically pleasing, then I think you're off to a good start!
Noldorin
I always wanted to write in language that has C-like syntax. But, I hate pointers and OOP stuff in C++ (I mean its OOP syntax), so I felt that C# might be a good one for me. Well I played around with VB.NET for around week and went back to VB6 until my friend suggested that VB6 is more like dead.
And I never liked Java because of it's slowiness (especially in GUI). The thing is that still don't use many of C# powers (like interfaces) and I write in C# for more than 1 year!
+4  A: 

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

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

arul
+2  A: 

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

michl86
A: 

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.)

Guffa
if (result = false) does not compile in C#. So you cannot do accidental writes in C#.
Jakob Christensen
@Jakob Christensen: It compiles just fine, you only get a warning.
Guffa
You're right. Scary :-)
Jakob Christensen
+1  A: 

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
*/
Randolpho
+1  A: 

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

nes1983