Rules for attributes

May 8, 2011 at 12:03 AM
Edited Jun 19, 2011 at 9:14 PM

Hi,

Current StyleCop rules want to ensure that every two c# elements are separated by blank line and none single c# element is splitted with unnecessary blank lines. However, current set can be easily fooled when attributes are involved. I'd like to propose few new rules regarding attributes:

  • ordering rule - element documentation headers must come before attributes applied to this element. This will help to keep "real" code together, not separated by documentation

Code like below would be penalized for wrong ordering:

[Browsable(false)]
/// 
/// Method description.
/// 
public void Method()
{
}
 
  • layout rule - attribute applied to element must not be followed by blank line - this rule is similar to SA1506

Code like below would be penalized for extra blank line:

[Browsable(false)]

public void Method()
{
}
 
  • layout rule - first attribute applied to element must be preceded by blank line, unless it is preceded by comment (regular or xml doc) or element is first in its scope - this rule is similar to SA1514

Code like below would be penalized for missing blank line:

public delegate void MyEventHandler(object sender, EventArgs ea);
[Browsable(false)]
public void Method()
{
}

 

In summary - I think both layout rules will help to ensure that c# elements are separated by blank lines and are not splitted with unnecessary blank lines. Also, ordering rule would be nice addition as  "real" code should be kept together, not separated by documentation.

Regards

Andy, any official statement?