Archives for : January2009

Often Unused Operators: |= (or equals)

I’ve debated before the usefulness of the | and & operators. The use of the | operator depends on the context in which it is used.

In the case of bools a boolean operation is performed. In the case of ints a bitwise OR is performed.

For boolean operations when the | operator is used both sides of the operator are evaluated. In the example below both VerifyStatus and SetDefaults are called regardless of the boolean returned by each of them.

public void TestTransaction(PaymentTransaction Transaction)
{
    if (Transaction != null && 
        (VerifyStatus(Transaction.State) | SetDefaults(Transaction)))
    {
        //do something with the code
    }
}
private bool VerifyStatus(ItemState State)
{
    if (State == null)
        return false;
    if (State.Status == Status.NotVerified)
        ValidateStatus(State);
    if (State.Status == Status.Valid)
        return true;
    //Status.Invalid
    return false;
}
private void ValidateStatus(ItemState State)
{
    //validate the status
}
private bool SetDefaults(PaymentTransaction Transaction)
{
    if (Transaction == null)
        return false;
    //no need to set the defaults if the state is already valid
    if (Transaction.State.Status == Status.Valid)
        return true;
    //set all the default values for the transaction
    return true;
}

Ok, I know it’s not a pillar of phenomenal code. But you get the point, both of the sides of the | are run. Now, what if you’ve all ready evaluated the status earlier in code? No need to do it again. But you may need to still set the defaults. Well, that is where the |= comes into play.

bool transStatus = VerifyStatus(Transaction.State);
//do something with the code
transStatus |= SetDefaults(Transaction);
if(transStatus)
{
    //do something with the code
}

In the case of an int a bitwise operation is performed.

int a = 0x0c;
a = a | 0x06;
//a results in 0x0000000e
//1100
//0110 results in
//1110 which is e

Like the logical operation example above the | and assignment can be collapsed down to:

int a = 0x0c;
a |= 0x06;

I think it’s very important to understand the difference between | and || as well as & and &&. Recently I came across a bunch of code where the original developer used | and || interchangeably. They are not the same. Using | in place of || overrides the fail fast nature of boolean operations most modern languages support. They are not interchangeable. If Transaction was null the following code would not work

if(Transaction == null | Transaction.State == null)

simply because both sides of the operation are evaluated so Transaction.State would throw a null pointer exception. Not only that but your code will run slower.

Next in this series will be checked and unchecked.

Later,
Brian

Linear Operations

<rant>

We all know O(n) = O(2n), right?  I mean, we’re talking about how algorithms scale here.  Linear is linear.  I add 100 more elements to my dataset and the time to finish what ever I was doing scales linearly.

Now that’s all fine and good but think a bit when working with code. Just because O(n) = O(2n) doesn’t mean that you should just throw loops around willy-nilly when with a bit of smarter design you can do O(n) instead of O(2n).

In the real world when iterating over datasets (especially potientially large datasets in my case) there is a big difference between O(n) and O(2n).

For example:

foreach(MyObjectType myObject in myList)
{
    //do something
}
foreach(MyObjectType myObject in myList)
{
    //do something
}

I know this may be a bit of an oversimplification but lately I’ve been looking at a lot of code that basically works out to the above. It’s really more like:

MyList stuffToProcessNext = new MyList();
foreach(MyObjectType myObject in myList)
{
    //do a lot of stuff here with a bunch more loops
    //rather then write some recursion to another method to handle children
    //and siblings just add to stuffToProcessNext
    stuffToProcessNext(myObject);
}
//now that MyObjects are processed and I don't have to worry about the
//siblings I can finish processing each of the children to MyObjects
foreach(MyObjectType myObject in myList)
{
    //do a lot of stuff here with a bunch more loops
    foreach(MyObjectType childObject in myObject.Children)
    {
        //do something relating to the siblings of myObject
    }
}

But don’t just look at your own code, understand how the underlying framework code works.
For example:

//assume myList is a List
//and myObject is MyObjectType
if(myList.Contains(myObject))
    myList.Remove(myObject);

No need to call Contains and force the framework to iterate over the list twice. Just remove it. No worries if it isn’t there.

</rant>

Please return to your regulary scheduled program.

Brian

Top 25 Most Dangerous Programming Errors

Follow the below link, read it, consume it, grok it.

2009 CWE/SANS Top 25 Most Dangerous Programming Errors (If it doesn’t come up be patient. It looks like the server is getting hit pretty hard with bloggers like myself linking to it.)

Or read a summary of it and then go to the link over at Coding Horror.

On a side note, now that the holidays have calmed down I’ve been working on a series of “Often Unused Operators” like ~ and |=. Credit goes to Jeff Clark for turning me onto this. Hopefully I’ll have the first one up on Friday.

Brian