What is the difference between ReSharper `MergeSequentialChecks` and` MergeSequentialChecksWhenPossible`?

I'm trying to figure out what is the difference between these two rules?

  • MergeSequentialChecks
  • MergeSequentialChecksWhenPossible

The documentation says nothing about the second. https://www.jetbrains.com/help/resharper/2016.1/MergeSequentialChecks.html

And I don’t understand what this means WhenPossible ?

If ReSharper suggests applying the first rule and combining my sequential checks, then it really is possible. How is this impossible?

Here is sample code to verify.

 public class Person { public string Name { get; set; } public IList<Person> Descendants { get; set; } } public static class TestReSharper { // Here `MergeSequentialChecks` rule is triggered for both `&&` operands. public static bool MergeSequentialChecks(Person person) { return person != null && person.Descendants != null && person.Descendants.FirstOrDefault() != null; } // Here `MergeSequentialChecksWhenPossible` rule is triggered. public static bool MergeSequentialChecksWhenPossible1(Person person) { return person != null && person.Descendants.Any(); } // Here `MergeSequentialChecksWhenPossible` rule is triggered. public static bool MergeSequentialChecksWhenPossible2(Person person) { return person.Descendants != null && person.Descendants.Any(); } } 
+7
c # resharper
source share
1 answer

The idea of ​​checking code labeled "(when possible)" is simple: we decided not to offer a possible code conversion with default R # settings, since the resulting code can lead to reduced readability or complicate understanding. This decision about what to offer or not is made using special heuristics.

When we first introduced the suggestions and code transformations related to C # 6.0 and reviewed their results in large solutions available to us, we decided that there were almost ~ 2/3 cases of checks, such as "Merging sequential checks" / "Use zero distribution "should not be offered to do code conversions. For example, we believe that in the case of code:

 if (node != null && node.IsValid()) { ... } 

... are there no real benefits to using an operator ?. and introducing the operator "raised" operator==(bool, bool) over a value of type bool? :

 if (node?.IsValid() == true) { ... } 

Plus, different developers have different ideas on how to check a value of type bool? for true (some prefer to have ?? false instead, but that will make the resulting code even?.more ?? questionable ).

Thus, in the case above, the merging of sequential checks is definitely "possible", but we will not recommend it with the default settings R # (the correct default settings are a huge responsibility), as a result of which the user can see all cases by including "(when it is possible) "is the version of checking the same code. As far as I can tell, we are currently checking to see if the removed Boolean checks are in the “Collapse sequential checks” mode, other checks have more heuristics, for example:

 if (stringBuilder != null) { // "Use null propagation" stringBuilder.Append("hello"); } 

Code above cost suggesting to use a conditional call statement:

 stringBuilder?.Append("hello"); 

When using a conditional call two or more times, it is doubtful ...

 if (stringBuilder != null) { // no suggestion stringBuilder.Append("hello"); stringBuilder.Append("world"); stringBuilder.Append("!!!"); } 

ps Contextual actions "Collapse sequential checks" / "For zero propagation" are always activated when conversion is possible, despite the status of the code checks and their severity.

+7
source share

All Articles