Double use of variables?

Posted by Vaccano on Stack Overflow See other posts from Stack Overflow or by Vaccano
Published on 2010-06-07T19:27:28Z Indexed on 2010/06/07 19:42 UTC
Read the original article Hit count: 164

Filed under:
|

I have read that a variable should never do more than one thing. Overloading a variable to do more than one thing is bad.

Because of that I end up writing code like this: (With the customerFound variable)

bool customerFound = false;
Customer foundCustomer = null;
if (currentCustomer.IsLoaded)
{
    if (customerIDToFind = currentCustomer.ID)
    {
        foundCustomer = currentCustomer;
        customerFound = true;
    }
}
else
{
    foreach (Customer customer in allCustomers)
    {
        if (customerIDToFind = customer.ID)
        {
            foundCustomer = customer;
            customerFound = true;
        }
    }
}
if (customerFound)
{
    // Do something
}     

But deep down inside, I sometimes want to write my code like this: (Without the foundCustomer variable)

Customer foundCustomer = null;
if (currentCustomer.IsLoaded)
{
    if (customerIDToFind = currentCustomer.ID)
    {
        foundCustomer = currentCustomer;
    }
}
else
{
    foreach (Customer customer in allCustomers)
    {
        if (customerIDToFind = customer.ID)
        {
            foundCustomer = customer;
        }
    }
}
if (foundCustomer != null)
{
    // Do something
}

Does this secret desires make me an evil programmer?

(i.e. is the second case really bad coding practice?)

© Stack Overflow or respective owner

Related posts about c#

Related posts about best-practices