11

FxCop 10 is complaining about the following:

using XYZ.Blah; //CA1709 - "XYZ"
using Xyz.Blah; //No complaint.

using XylophoneSuperDuperLongFullName.Blah; //I don't want to have a long full name for my company name.

The problem is... I want my company name to show up in all UPPERCASE because XYZ is an abbreviation. The long version of the name is much too long to be a useful namespace. Microsoft gets away with this kind of stuff because their acronym is only 2 letters.

using MS.Something; //No Complaint.
using Microsoft.SomethingElse; //No Complaint.

So, I was looking at adding a SuppressMessageAttribute to suppress this warning. But, I'm not sure how to do so properly to only (or where to even stick it) so that it ONLY affects this one instance. I don't want to Suppress anything within that namespace because I want to catch any other mistakes I make. I did look at at the msdn and google searched but I can't find anything that shows how to specifically just target this instance. The closest I found was Scope = "namespace" but I wasn't sure if that means it affects the actual namespace name or if it affects everything WITHIN that namespace.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
michael
  • 14,844
  • 28
  • 89
  • 177

3 Answers3

15

MSDN - CA1709: Identifiers should be cased correctly:

It is safe to suppress this warning if you have your own naming conventions, or if the identifier represents a proper name, for example, the name of a company or a technology.

You can also add specific terms, abbreviations, and acronyms that to a code analysis custom dictionary. Terms specified in the custom dictionary will not cause violations of this rule. For more information, see How to: Customize the Code Analysis Dictionary.


That being said, if you feel justified to suppress the message, it really isn't hard at all. In FxCop 10 right click on any message you want to suppress and go to Copy As>Suppress-Message or Copy As>Module-level Suppress Message.

You should place the SuppressMessageAttributes in the appropriate locations. Attributes that suppress a single location should be placed on that location, for example, above a method, field, property, or class.

In you're instance, there is no specific location to place the attribute (by default it should copy over as [module: SuppressMessage(...)]. This is a good indication that it belongs either at the top of a file if it is a module-level suppression particular to a file (for example, to a resource specific to a file). Or, and more likely, it belongs in a GlobalSuppressions.cs file.

using System.Diagnostics.CodeAnalysis;

[module: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Justification = "Because I said so!", MessageId = "XYZ", Scope = "namespace", Target = "XYZ.Blah")]

You can also shorten the CheckId property if you want to, but it's good to know what CA1709 means. If you don't feel like it, this also works:

using System.Diagnostics.CodeAnalysis;

[module: SuppressMessage("Microsoft.Naming", "CA1709", Justification = "Because I said so!", MessageId = "XYZ", Scope = "namespace", Target = "XYZ.Blah")]

And lastly... all this will be fruitless unless you include the 'CODE_ANALYSIS' symbol in your build. Go to Properties>Build and add the conditional compilation symbol.

myermian
  • 31,823
  • 24
  • 123
  • 215
  • 3
    +1. They key here, which I never noticed in any other FxCop documentation, is the `CODE_ANALYSIS` symbol. I didn't know that had to be added in VisualStudio, and couldn't figure out why FxCop wasn't ignoring anything! – CodingWithSpike Jan 24 '12 at 17:40
6

Acryonyms aren't meant to be all upper case in .NET naming conventions. For example HttpResponse etc.

From the capitalization conventions:

Casing of acronyms depends on the length of the acronym. All acronyms are at least two characters long. For the purposes of these guidelines, if an acronym is exactly two characters, it is considered a short acronym. An acronym of three or more characters is a long acronym.

The following guidelines specify the proper casing for short and long acronyms. The identifier casing rules take precedence over acronym casing rules.

Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.

A property named DBRate is an example of a short acronym (DB) used as the first word of a Pascal-cased identifier. A parameter named ioChannel is an example of a short acronym (IO) used as the first word of a camel-cased identifier.

Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier.

A class named XmlWriter is an example of a long acronym used as the first word of a Pascal-cased identifier. A parameter named htmlReader is an example of a long acronym used as the first word of a camel-cased identifier.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Oh, I know. There are exceptions though: "DB" for example. Aesthetically though, I find it more readable to see `XYZ.Blah` than `Xyz.Blah`. Especially for the abbreviated company name. Hence... I want to suppress the message for just that instance. :) – michael Jun 17 '11 at 14:03
  • Oh, and I'm sorry. I meant "abbreviation" not acronym. My company name is abbreviated as XYZ. I'll fix that. – michael Jun 17 '11 at 14:04
  • @Michael: It's even stranger for an *abbreviation* to be all capitals, to be honest. Note that the exceptions are covered in the documentation, and I'd expect StyleCop to know that. While *you* may find it more readable to see XYZ.Blah, I'd certainly find the opposite - being used to the normal .NET conventions. – Jon Skeet Jun 17 '11 at 14:48
  • Well, regardless. How can I suppress that warning message? – michael Jun 17 '11 at 15:04
  • @michael: Assuming `#pragma warning disable` doesn't work (which I suspect it won't) I don't know of a way of disabling StyleCop warnings on a "region of code" basis. – Jon Skeet Jun 17 '11 at 15:07
0

If you were checking names via StyleCop, you could use StyleCop+ (custom rules) which supports configurable abbreviations list.

Oleg Shuruev
  • 1,339
  • 8
  • 10