Just a question on preference:

Given a method that returns a boolean value, how would you write an If statement to check the return value of it?

Like this?

private bool MyMethod()
{
    // doing stuff
}

private void OtherMethod()
{
    If (MyMethod() == true)
    {
        // do stuff if true
    }
    else
    {
        // do stuff if false
    }
}

like this?

private bool MyMethod()
{
    // doing stuff
}

private void OtherMethod()
{
    If (MyMethod())
    {
        // do stuff if true
    }
    else
    {
        // do stuff if false
    }
}

some other way?

Any reason why you would do it one way over the other?

Between those two, option two. Otherwise, it depends on the method. If it does validity checks, the method might be named:

private bool ItemExists()

making it clear enough without using == true.

However if your method does processing that can fail (and that is why you return true for success, or false otherwise), then I'd prefer exceptions instead:

try
{
    MyMethod();
}
catch
{
}

Agree
Coding <some boolean expression> == true is a novice mistake. However the name of the method should include a verb and clearly express some assertion with obvious meanings for true/false, eg ItemExists, IsValid, HasMore, MeetsEPARegulations, WasAchievedByCheating etc

Any reason why you would do it one way over the other?

Three words: don't repeat yourself. Saying if (x == true) is redundant. The context alone tells you that x represents a boolean. If you're being redundant "just in case", then would that be an argument for stuff like this?

if (MyMethod() == true == true)

;)

In the real world there are a lot of half true things. What do you do with those?

Second example simply because it's 8 or so less keystrokes.

In the real world there are a lot of half true things. What do you do with those?

Try fuzzy logic

1) if(Method()==true)
in this case code under if will only run if Method() return boolean true

2) if(Method())
in this case code under if will always run if Method() return any value. It will not run for false/blank/null.

MyMethod was defined as returning a bool.
how would it return anything other than true or false?

In C# a bool method always returns a boolean value. In other languages such as PHP this is not necessarily the case, but since this question was in regards to C# I will answer as such.

I think your question is about personal preference so there isn't really a right or wrong answer. For mine, when checking for true I always omit the == true. I actually think that omitting the unnecessary code improves readability of your code. It is also important that methods and properties have a strong naming convention in your code though.

However when I am checking for false I sometimes use the NOT symbol (for example, !someBool) and sometimes use a more explicit someBool == false depending on the complexity of the algorithm. Sometimes when you are reading code back a few years later it is easy to miss that exclamation mark that indicates a check for NOT, whereas the second method indicates clearly that you are checking for false.

commented: Good point re checking for false +15

In If statement a Boolean variable/method/functon always checks True, by default. But, when you call it alone or attach a ! with it, it depands on the structure of your coding style or on your requirment.

If you have a method call alone as the condition you can rightly assume that the method returns bool since C# has no implictit conversion to bool.

My 2 cents. I used to do == false or == true. Now I do the 2nd option (looks cleaner, reducing clutter)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.