Domanda

Consider the following arithmetic calculations. I have shortened them as much as possible for the sake of simplicity:

x = y + y * z;

x = y + z - y * z;

This is perfectly legal without StyleCop, and presumably, the calculation follows BODMAS (as should any arithmetic calculation).

When this is passed through StyleCop, it shows up the following warning:

CSharp.Maintainability : Insert parenthesis within the arithmetic expression to declare the operator precedence.

Presumably, all I need to do is follow the rules of BODMAS when inserting brackets, like so:

x = y + (y * z);

x = (y + z) - (y * z);

As I correct in my assumption that StyleCop expects me to explicitly define the rules of BODMAS if I want the calculation to be performed, exactly as it would without brackets?

È stato utile?

Soluzione

It is correct. StyleCop basically assumes you are not too comfortable with Operator precedence and wants that explicitly written out. That can lead to more stable code if someone works on the formulas later. Iti s technically not needed - but then you assume to be absolutely correct in your assumptions how the Compiler will handle that. Naturally that normally is the case (BODMAS).

At the end, it is a case of "you may not know what you do, so we make sure you spell that out". It is not a bad attitude - code stability IS The main concern - but you could as well just turn that warning off.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top