Search code examples
c#performanceasp.net-4.0code-readabilityboolean-operations

how to decide on a bool operators usage, performance issues vs readability


Having even more than two options to choose from, leads me to question, which one to choose, if the result / outcome is same.

in .NET C# the following conditions are asking same question using different operators, so the question is , what experienced developers use, i tend to assume that ready made operators like Equals would go through more processing actions.

When and why would you choose ! over Equals, and both over 'traditional' == ?

//bool
if (!Page.IsPostBack) 
{
    //bool
    if (NotAuthorized().Equals(false)) 
    {            
        AppsCtrls.DDLs_Init();

        //bool
        if (CurrSeSn.Raised(Flag.MainDataSet_IsPopulated) == false)
        {
            initALLDataSet(AllDataStColsSelectionMod.doneViaSP);
        }

        custid = RConv.Str2int(Request.QueryString["custid"]);
        username = GetTableData.AsString("name", "tblCustomers", "custid", custid);
    }
}

Solution

  • This question is somewhat subjective...

    "i tend to assume that ready made operators like Equals would go through more processing actions."

    " when and why would you choose ! over Equals , And both over 'traditional' == "

    the Equals method as part of an instance of an object is used to check for equality of that instance against another, whilst the == and != operators are static and are therefore unbound from any object instance. Instead these are like a special static method that accepts two arguments (of usually the same type) and compares them.

    Consider the following example:

    public class CustomObject
    {
        int someValue, anotherValue;    
    
        public bool Equals(CustomObject obj)
        {
            return (this.someValue == obj.someValue && this.anotherValue == obj.anotherValue);
        }
    
        public static bool operator ==(CustomObject a, CustomObject b)
        {
            return a.Equals(b);
        }
    
        public static bool operator !=(CustomObject a, CustomObject b)
        {
            return !(a == b);
        }
    }
    

    In this example, the Equals method is used to produce a result of the comparison of values in the CustomObject against another instance of the same type. The == operator for CustomObject simply calls Equals on one of the parameter objects and performs an equality check against the other. The != operator simply negates the == and produces the opposite result. Therefore, == and != do not have much performance overhead over Equals, because they both call that method anyway.

    Best practices:

    if a condition is boolean by nature there is no need to use Equals, != or ==, but you should use ! to negate a boolean condition.

    For example:

    if(IsPostBack) // this is good
    
    if(IsPostBack == true) // this is unnecessary
    
    if(!IsPostBack) // this is good
    
    if(IsPostBack == false) // this is unnecessary
    

    If a condition is not boolean by nature, or you are comparing two boolean values, or you are comparing an enumeration, or other value type then use of != or == is acceptable.

    For example:

    if(a == b) // this is good
    
    if(a != b) // this is good
    

    If a condition is not boolean by nature and the objects you are comparing do not have the == or != operators implemented, then using Equals is acceptable. Note, using != and == are prohibited with generics since it is not known at compile time that != or == are implemented on the objects represented by the generic objects type parameters.

    For example:

    if(a.Equals(b)) //this is good
    
    if(!a.Equals(b)) // this is good
    
    if(a.Equals(b) == true) // this is unnecessary