vendredi 27 juillet 2018

Refactorisation of an if Block

In the following code I have 2 if block.
A, The first return the error when the object is null.
B, the second will try to send the object and if it' fails then return and error. It should be executed only if not A, the object is not null.

private bool SendToERP(Foo foo, out string error)
{
    error = "";
    var ticketIn = TicketFromFoo(foo, out string err);

    if(ticketIn == null)
    {
        error = err;
        return false;
    }

    if ( !GenericERP_TicketSubmit(ticketIn, out err))
    {
        error = err;
        return false;
    }

    return true;
}

The action after those condition are the same and I want to refactor on into a unique if block.

As I can't warp my head 2 condition and an and I wrote a simple truth table to help me. But it didn't help me mutch.

Aucun commentaire:

Enregistrer un commentaire