Optimize ordering rules like SA1203: All constants must be placed before all non-constants.

Jun 19, 2012 at 8:47 PM

Some class has such members:

public const

public static readonly

public const

... 100 more ...

public const

StyleCop generates 100 violations SA1203: All constants must be placed before all non-constants instead of 1.

Coordinator
Jun 19, 2012 at 9:01 PM

Yes. You have 102 constants in the wrong place.

Jun 19, 2012 at 9:24 PM

That's correct. On the other hand I have only 1 non-constant in the wrong place.

Coordinator
Jun 19, 2012 at 9:27 PM

Nope. You have only 1 public static read-only field in the wrong place (a non-constant) - but that's not the reported violation. The violation is the constants in the wrong place. Either way, they need fixing.

Jun 19, 2012 at 9:34 PM

You are right. The reported violation is, technically, correct. However it would be more natural to report just 1 violation for non-constant, even with the same code and text - SA1203: All constants must be placed before all non-constants.

Coordinator
Jun 19, 2012 at 9:36 PM

Maybe - but that's not how it works. Consider the revers e-  you'd be suggesting it needs to be the other way round. It compares pairs of elements and moves through all the fields.

Jun 20, 2012 at 6:59 AM
Edited Jun 20, 2012 at 7:00 AM

Hi

I write my problem here since it seems similar. The following code:

 

private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(1);

private static bool initialized;

private readonly object instanceLocker = new object();

private bool isStarted;

 

Gives me no warning with stylecop + resharper, but policy evaluation for TFS checkin fails (violated rule: 1203). Where's the problem?

Thanks

Coordinator
Jun 20, 2012 at 7:08 AM

SA1203 is constants before fields. The code above has no constants. It suggests that you have a different version of StyleCop installed somewhere as older versions of StyleCop would throw SA1203 for this.