superted
superted

Reputation: 325

Simplifying IF statement logic - inner logic overlap

Can the below logical if conditions be simplified ? I have wrote this code but some parts are overlapping so I thought to seek for some help to see whether it can be simplified...

I have actually three different fields but following the same patterns.

EDIT :

    if (Row.ReceivableAmount_IsNull == true && Row.CustomerID == LastCustomerID)
    {
        if (LastReceivableAmount == null)
        {
            Row.PreviousReceivableAmount_IsNull = true;
        }
        else
        {
            Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
        }
    }
    else
    {
        Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
        LastReceivableAmount = Row.ReceivableAmount;
    }

    if (Row.SaleAmount_IsNull == true && Row.CustomerID == LastCustomerID)
    {
        if (LastSaleDate == null)
        {
            Row.PreviousSaleDate_IsNull = true;
        }
        else
        {
            Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
        }
    }
    else
    {
        if (LastSaleDate == null)
        {
            Row.PreviousSaleDate_IsNull = true;
        }
        else
        {
            Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
        }

        LastSaleDate = Row.Date;
    }

    if (Row.PaymentAmount_IsNull == true && Row.CustomerID == LastCustomerID)
    {
        if (LastPaymentDate == null)
        {
            Row.PreviousPaymentDate_IsNull = true;
        }
        else
        {
            Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
        }
    }
    else
    {
        Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
        LastPaymentDate = Row.Date;
    }

Upvotes: 2

Views: 201

Answers (2)

David L
David L

Reputation: 33815

Yes, you only care about LastSaleDate in your outer if condition, so move everything else out.

Once you've moved it out, you can invert your original condition, reducing your if/else to just an if.

if (LastReceivableAmount == null)
{
    Row.PreviousReceivableAmount_IsNull = true;
}
else
{
    Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
}
if (!Row.ReceivableAmount_IsNull || Row.CustomerID != LastCustomerID)
{
    Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
    LastReceivableAmount = Row.ReceivableAmount;
}


if (LastSaleDate == null)
{
    Row.PreviousSaleDate_IsNull = true;
}
else
{
    Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
if (!Row.SaleAmount_IsNull || Row.CustomerID != LastCustomerID)
{
    LastSaleDate = Row.Date;
}   


if (LastPaymentDate == null)
{
    Row.PreviousPaymentDate_IsNull = true;
}
else
{
    Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
}
if (!Row.PaymentAmount_IsNull == true || Row.CustomerID != LastCustomerID)
{
    Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
    LastPaymentDate = Row.Date; 
}

Upvotes: 1

Dmitry
Dmitry

Reputation: 14059

Since both branches of the if are similar, except one statement, you could use the following approach:

if (LastSaleDate == null)
{
    Row.PreviousSaleDate_IsNull = true;
}
else
{
    Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}

if (!Row.SaleAmount_IsNull || Row.CustomerID != LastCustomerID)
{
    LastSaleDate = Row.Date;
}

Upvotes: 1

Related Questions