Comments (1)
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)
- Encodo Style Settings Files as public Nuget HOT 2
- Add warning about records vs. struct records
- Settings Files (StyleCop , ReSharper and EditorConfig.) HOT 3
- Update to include advice for C# 8
- Include section on how to create transient objects
- Add FAQ / tl;dr at the beginning of the document
- Add section on global usings
- Add notes on using statements (without braces)
- Add warning about using parameterless constructors in structs (C# 10)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csharphandbook.