Skip to content

Add advice on ternary operators and code coverage #4

@mvonballmo

Description

@mvonballmo

Every code-coverage tool I've seen tracks coverage by lines. A ternary operator or coalescing operator places multiple branches on a single line.

For example, the following code looks quite verbose.

if (expression != null)
{
  if (otherBaseExpression == null)
  {
    return false;
  }

  return expression.Equals(otherBaseExpression);
}

if (otherBaseExpression != null)
{
  return false;
}

return Equals(BaseValue, otherDynamicType.BaseValue);

While we could rewrite the code as follows, tightening things up a bit and not losing too much legibility, we would lose the line-by-line coverage we had before. That is, we won't be able to tell from our coverage whether each branch was really tested.

if (expression != null)
{
  return otherBaseExpression != null && expression.Equals(otherBaseExpression);
}

return otherBaseExpression == null && Equals(BaseValue, otherDynamicType.BaseValue);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions