SA1009 and Explicit Type Casting

Aug 25, 2010 at 1:55 PM

I wonder why the following code generate StyleCop warning around the closing parenthesis of the cast (in the line 2):

object x = 1;
int y = (int) x;

Shouldn't a space exist between (int) and x?

Thanks.

Aug 25, 2010 at 2:30 PM

If you right-click on the warning in the Error List window and select Show Error Help it'll tell you.

Then you can turn that one off if you want.

 

Aug 25, 2010 at 4:29 PM
Edited Aug 25, 2010 at 4:31 PM

Actually, I'm asking the reason, why it's that way. I also notice that the default VS C# text editor configuration has "insert space after cast" unchecked by default.

In my opinion and based on my experience, with space is easier to read than without space.

Aug 25, 2010 at 9:28 PM

There is not much of a why, other than the the majority of C# devs today seem to default to no space, and VS also default to no space. StyleCop goes for consistency, so it also goes for no space.

Developer
Aug 25, 2010 at 9:45 PM

In my opinion for "why":

Foremost:  Visual Studio auto-formatting removes the space.  Using edit -> advanced -> format document should always work towards the enforced consistency rather than against it.  I imagine other IDEs and formatting tools would auto-format similarly.  No sense in applying a rule that is fought by common tools.

A distant secondary reasoning: The casting is highly related to the thing on the right.  It would look odd to have a method call like "foo ( bar )" or "foo (bar)" or even "foo((int) bar, x, y, z)" as each of these mentally applies a separation of related things rather than a binding of the highly related things?  But really, ignore this reason as the first is more than enough IMO  :)

Aug 26, 2010 at 2:31 PM

IC. Thanks for the explanation.

Also, I initially tought that this StyleCop rule setting has something to with VS setting. By that, it means that by changing VS option, it would not generate the warning. But, it turns out it's not the way around. Is it possible to have StyleCop setting parameterized and integrated with VS setting?

Aug 30, 2010 at 4:41 PM

Those types of settings are per IDE, they aren't stored inside the project files. So if developer A adds a space and developer B does not have the same Visual Studio settings, StyleCop on his machine would indicate an error.

Aug 30, 2010 at 5:25 PM

The method I use to solve that particular issue is to check in a "StyleCop.vssettings" file into source control in the same place the solution-level Settings.StyleCop file is checked in.

Anyone who finds that their Visual Studio settings are incorrect for the StyleCop solution settings can use that handy file to easily configure Visual Studio to the correct settings.

 

Sep 2, 2010 at 10:01 AM
Edited Sep 2, 2010 at 10:02 AM

I don't think that's not what I meant. I'm thinking about having a setting linkage between StyleCop individual setting and VS individual formatting setting.

For example, in my case, I see that there's actually a linkage between SA1009 setting and VS C# formatting setting "insert space after cast". That way, SA1009 should respect VS C# formatting setting to determine whether it's valid or not. If "insert space after cast" was unchecked, SA1009 validation should report error; but when checked, it should report it as being valid. Thus, in my term, it's like to have StyleCop setting parameterized according to VS setting.