Stylecop burst my bubble
-
So, I decided that I would download Stylecop and see what sort of issues it took with my code. I found a couple of things surprising, one frustrating. My frustration was that I'm not allowed to pad with spaces? For a long time now, I've enjoyed setting up Visual Studio to add spaces in empty parentheses, spaces in method calls etc. I've just found the code to be all the more readable. But, following Stylecop rules, I've got no extra room. I mean, this will please the people I work with, they don't like the spaces but I've come to like it.
if ( value < maximumValue ) { }
I've found to be more readable than;if(value < maximumValue) { }
My next surprise was that it was telling me to put my using statements inside my namespace. I've not seen this done before in code samples I've seen even from MS,and considering this tool comes out of Microsoft it took me by surprise. Can anybody explain this one to me? I understand the idea of Stylecop and I'm willing to follow most. Is there a way of editing the rules? -
So, I decided that I would download Stylecop and see what sort of issues it took with my code. I found a couple of things surprising, one frustrating. My frustration was that I'm not allowed to pad with spaces? For a long time now, I've enjoyed setting up Visual Studio to add spaces in empty parentheses, spaces in method calls etc. I've just found the code to be all the more readable. But, following Stylecop rules, I've got no extra room. I mean, this will please the people I work with, they don't like the spaces but I've come to like it.
if ( value < maximumValue ) { }
I've found to be more readable than;if(value < maximumValue) { }
My next surprise was that it was telling me to put my using statements inside my namespace. I've not seen this done before in code samples I've seen even from MS,and considering this tool comes out of Microsoft it took me by surprise. Can anybody explain this one to me? I understand the idea of Stylecop and I'm willing to follow most. Is there a way of editing the rules?I've never used it but a few comments on what you've found.
hammerstein05 wrote:
pad with spaces
Screen space is a highly prized resource IMO. Extra spaces (horizontal especially as vertical can be conrtolled with collapsing sections/regions etc) drive me nuts when working on a laptop or smaller than ideal display.
hammerstein05 wrote:
using statements inside my namespace
I never do this, but it makes sense as the usings only apply to objects inside that namespace.
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
So, I decided that I would download Stylecop and see what sort of issues it took with my code. I found a couple of things surprising, one frustrating. My frustration was that I'm not allowed to pad with spaces? For a long time now, I've enjoyed setting up Visual Studio to add spaces in empty parentheses, spaces in method calls etc. I've just found the code to be all the more readable. But, following Stylecop rules, I've got no extra room. I mean, this will please the people I work with, they don't like the spaces but I've come to like it.
if ( value < maximumValue ) { }
I've found to be more readable than;if(value < maximumValue) { }
My next surprise was that it was telling me to put my using statements inside my namespace. I've not seen this done before in code samples I've seen even from MS,and considering this tool comes out of Microsoft it took me by surprise. Can anybody explain this one to me? I understand the idea of Stylecop and I'm willing to follow most. Is there a way of editing the rules?You can turn off some of Style Cops annoyances. Besides, you should be spending more time focusing on the logic and functionality of your code than the style. In fact, one of the first things I do in a contract that involves code reviews is insert an obvious error into my code for review. If this first comment about my code is style related I know that most people in the room are not playing for the right team. It is about code first.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
I've never used it but a few comments on what you've found.
hammerstein05 wrote:
pad with spaces
Screen space is a highly prized resource IMO. Extra spaces (horizontal especially as vertical can be conrtolled with collapsing sections/regions etc) drive me nuts when working on a laptop or smaller than ideal display.
hammerstein05 wrote:
using statements inside my namespace
I never do this, but it makes sense as the usings only apply to objects inside that namespace.
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)DaveyM69 wrote:
hammerstein05 wrote: using statements inside my namespace I never do this, but it makes sense as the usings only apply to objects inside that namespace.
I asked the StyleCop folks about this and that was their answer, to limit the using statements to apply only to the objects inside the namespace. Their assumption is that you would only have one namespace per file, so I assume that there would also be a stylecop warning for multiple namespaces in a single file. Never tried it to validate this assumption though.
Mike Poz
-
DaveyM69 wrote:
hammerstein05 wrote: using statements inside my namespace I never do this, but it makes sense as the usings only apply to objects inside that namespace.
I asked the StyleCop folks about this and that was their answer, to limit the using statements to apply only to the objects inside the namespace. Their assumption is that you would only have one namespace per file, so I assume that there would also be a stylecop warning for multiple namespaces in a single file. Never tried it to validate this assumption though.
Mike Poz
Not questioning you, but the StyleCop folks: if they're assuming that you'll only have one namespace in a file, what does it matter if the usings are inside or outside the namespace?
-
Not questioning you, but the StyleCop folks: if they're assuming that you'll only have one namespace in a file, what does it matter if the usings are inside or outside the namespace?
Take a look at the document here[^]. In a nutshell: There are subtle differences between placing using directives within a namespace element, rather than outside of the namespace, including: 1. Placing using-alias directives within the namespace eliminates compiler confusion between conflicting types. 2. When multiple namespaces are defined within a single file, placing using directives within the namespace elements scopes references and aliases.
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
So, I decided that I would download Stylecop and see what sort of issues it took with my code. I found a couple of things surprising, one frustrating. My frustration was that I'm not allowed to pad with spaces? For a long time now, I've enjoyed setting up Visual Studio to add spaces in empty parentheses, spaces in method calls etc. I've just found the code to be all the more readable. But, following Stylecop rules, I've got no extra room. I mean, this will please the people I work with, they don't like the spaces but I've come to like it.
if ( value < maximumValue ) { }
I've found to be more readable than;if(value < maximumValue) { }
My next surprise was that it was telling me to put my using statements inside my namespace. I've not seen this done before in code samples I've seen even from MS,and considering this tool comes out of Microsoft it took me by surprise. Can anybody explain this one to me? I understand the idea of Stylecop and I'm willing to follow most. Is there a way of editing the rules?You can selectively enable/disable most of the rules but there isn't a way to edit a rule. Putting using statements inside the namespace is recommended for the following reasons:
From http://www.thewayithink.co.uk/stylecop/sa1200.htm[^] There are subtle differences between placing using directives within a namespace element, rather than outside of the namespace, including: 1. Placing using-alias directives within the namespace eliminates compiler confusion between conflicting types. 2. When multiple namespaces are defined within a single file, placing using directives within the namespace elements scopes references and aliases.
The spacing reccomendations are most likely due to screen space concerns. Anyway, the code you show would look like this to meet the StyleCop settings:
if (value < maximumValue)
{
}Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai