There is a C# rule S1067. A rule like this could be useful. But the existing rule is pretty useless for us. We had to disable it because around 95% of the issues it created were easily readable code that would have been worse it we would have “fixed” it.
A similar, but better rule idea we could use: Don’t combine ‘logical AND’ and ‘logical OR’ too often.
Compliant (because it’s easily readable):
return MyClass.Get1stValue()
&& MyClass.Get2ndValue()
&& MyClass.Get3rdValue()
&& MyClass.Get4thValue()
&& MyClass.Get5thValue();
return MyClass.Get1stValue()
|| MyClass.Get2ndValue()
|| MyClass.Get3rdValue()
|| MyClass.Get4thValue()
|| MyClass.Get5thValue();
catch (Exception ex) when (ex is IOException || ex is ArgumentException || ex is NotSupportedException || ex is UnauthorizedAccessException || ex is SecurityException)
Non-Compliant (because it’s not easily readable and in this case, putting parts of the logic into separate methods (what S1067 suggests) would actually be useful.):
return (MyClass.Get1stValue()
|| MyClass.Get2ndValue())
&& (MyClass.Get3rdValue()
|| MyClass.Get4thValue())
&& MyClass.Get5thValue();
An example, how to “fix” this code:
var a = MyClass.Get1stValue() || MyClass.Get2ndValue();
var b = MyClass.Get3rdValue() || MyClass.Get4thValue();
return a && b && MyClass.Get5thValue();
How it could be implemented?
If there is only || or only && within a logical expression, do nothing.
Else: If the total number of logical operators is higher than $THRESHOLD$, it’s an issue.
The default value for $THRESHOLD$ could be 3 (as in S1067).