StyleCop fixes may be breaking changes, part this.X of this.Y

I seem to have an ongoing series about Practical Software Masochism™ where I describe the practical problems of my adherence to .

Today’s rule of horror is:

SA1202: All private fields must be placed after all internal fields.

Which is a perfectly reasonable demand to make and I had little reason to disagree that

Complying with a standard ordering scheme based on access level can increase the readability and maintainability of the file and make it easier to identify the public interface that is being exposed from a class.

But it causes a conflict with the C# specification regarding static and instance field initialization (10.5.5.1, 10.5.5.2, pp. 293ff. for Version 3.0). In particular

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration.

Personally, I find it reasonable to initialize internal or public readonly fields with values defined by private readonly fields, e.g. special values for a certain class, that is, things like DateTime.MaxValue.

An example of this is Pitfall: Static Field Inline Initialization Order of Execution.

So we need to silence StyleCop on this one:

[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess",
            Justification = "Textual order of static initializers is significant (C# 3.0 specification 10.5.5.1).")]

Advertisements
This entry was posted in Coding Horror. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s