mardi 17 novembre 2020

Branch coverage without branching - I know sounds weird

I have a bunch of validation code that follows this pattern:

bool IsBroken()
{
    var isBroken = Check(..., "error-1")
        | Check(..., "error-2")
        ...
        | Check(..., "error-n");

    if (...)
    {
        isBroken |= Check(..., "error-1")
            | Check(..., "error-2")
            ...
            | Check(..., "error-n");
    }
    else
    {
        isBroken |= Check(..., "error-1")
            | Check(..., "error-2")
            ...
            | Check(..., "error-n");
    }

    // ...
    // Could have more if-else or even nested if-else

    return isBroken;
}

bool Check(bool condition, string message)
{
    if (condition)
    {
        errors.Add(message);
    }
    return condition;
}

The goal is to perform all checks regardless of the outcome. The problem with this code however, is that it's not branch coverage friendly.

A simple fix would be to replce Check(bool, string) with the function body obviously but this creates another lesser problem which is code clutter. I am trying to figure out a solution that can give me the best of both worlds. Any ideas?!

Aucun commentaire:

Enregistrer un commentaire