Giter VIP home page Giter VIP logo

Comments (1)

mvonballmo avatar mvonballmo commented on May 18, 2024

This applies not just for code coverage, but also for debugging and setting breakpoints.

It's not so easy to test the return value of Equals because the value constantExpression in the pattern-matching expression will show as null in the debugger until you've evaluated it. In this case, there isn't usually a way to execute only that expression (even in Rider), so you end up stepping over the whole line.

return obj is ConstantExpression constantExpression && Equals(ConstantValue, constantExpression.ConstantValue);

If you write the code with an if instead, as follows, then the code is easier to debug (and check for code coverage).

if (obj is ConstantExpression constantExpression)
{
  return Equals(ConstantValue, constantExpression.ConstantValue);
}

return false;

Similarly, the functional style that applies a mapping operation can also be difficult to debug. It's highly compact and legible, but it's very difficult to set a breakpoint. The Zip line below, applies the function Equals for the corresponding arguments in the two lists Nodes and Expression.Nodes and then returns a value indicating whether they are all true.

if (obj is IExpression otherExpression && GetType() == otherExpression.GetType())
{
  return Nodes.Zip(otherExpression.Nodes, Equals).All(o => o);
}

return false;

It is nearly impossible to break anywhere in this expression.

If you make it even more compact, then it's not only illegible, it's not debuggable and not coverable.

return obj is IExpression otherExpression && GetType() == otherExpression.GetType() && Nodes.Zip(otherExpression.Nodes, Equals).All(o => o);

A more debuggable version would be to unroll the operation so that you can set a breakpoint.

if (obj is IExpression otherExpression && GetType() == otherExpression.GetType())
{
  foreach (var (o1, o2) in Nodes.Zip(otherExpression.Nodes, (o1, o2) => (o1, o2)))
  {
    if (!Equals(o1, o2))
    {
      return false;
    }
  }

  return true;
}

return false;

It's not nearly as pretty and it allocates tuples for each item (instead of booleans), but it is more debuggable. Prefer the shorter (but not the shortest) form if there is strong need for debugging or coverage. If the need arises, you can always rewrite individual instances.

from csharphandbook.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.