Error:Identifiers should not contain underscores

Hi,

When I code i use underscores in variables, such as m_sString. m for member and s for string followed by the name. I use this i class names and in namespaces as well.

I have started to use FxCop to analyze my code and it complains on my underscores with the phrase: Identifiers should not contain underscores.

Questions:

1. Does anyone knows why I can't use underscores
2. What will happen if I use underscores
3. Does anyone have another suggestion for naming conventions regarding my topic

Best
/M



Answer this question

Error:Identifiers should not contain underscores

  • Yasir Imran

    This rule is simply telling you that if you are developing a reusable library, that using underscores in an API causes your library to feel un-.NET-like. If you are simply developing an application, feel free to turn the rule off and use underscores.

  • gidyeo

    FxCop design and naming rules follow the Framework Design Guidelines, a document that provides guidelines for developing class libraries that target the .NET Framework.

    However, with these Design and Naming rules, FxCop will not analyze internal/private code.

    What visibility is 'm_aString'



  • stallion_alpa

    Thanks Todd, it looks like this has already been updated a bit in FxCop v1.36 (beta 2):

    Info : "Do not use underscores when specifying identifiers.

    Some generated Visual Studio identifiers for applications

    contain underscore characters. Underscore characters

    should generally be avoided in public identifiers."

    This gives me a couple of hints on when I should or shouldn't worry about this warning, i.e.:
    • If you believe that the underscores were generated by Visual Studio (e.g. if you can't find any underscores in the source code being flagged, but they still show up in the MSIL that is examined by FxCop), then it should generally be safe to ignore this warning.
    • If you have explicitly placed underscores in your source code, AND the underscores are being used in the names of Methods (e.g. Functions, Subs), Properties, Structures and other objects [i.e. those object names that someone else might have to type into their code, if they used your assembly as an Include], then remove the underscores from the names if the object is marked with the Public modifier.
    Am I interpreting this guidance correctly If so, I'll gladly add it to the Community Content section for this rule on MSDN.
    Cheers, Mike


  • Thomas S. Andersen

    Hi Mike,

    In our next release we are going to be working on improving our guidance around when to use which rules and we'll also be sure to update our docs with more details. Thank you for bringing this point up. That being said if you are developing a reusable library I would not disable this rule. People sometimes under estimate the value of having a consistent naming scheme.

    -Todd



  • Liam404

    Well I'm sorry for the inconvenience Mike. Hopefully I'll get some time to take a closer look at your code and figure out what is going on there. For now you should probably just use assembly level suppression attributes to suppress these FxCop violations.

    -Todd



  • nullptr

    Todd, thanks again - this is very helpful.
    I thought for sure you'd nailed it, but unfortunately neither of the two suggestions eliminated these false positives (or what I'm alleging are "false positives" anyway):
    1. Enabling the Project > Options > Spelling & Analysis > Suppress analysis results against generated code" didn't eliminate the two original warnings - they're still there.
    2. I checked, and "Run all overridable rules against all targets" was NOT enabled. When I enabled it, it added another eight warnings to the two I had already.
    The warning text for these is:

    Target : Word2MediaWiki__.W2MWPP_UI_Config (IntrospectionTargetType)

    Resolution : "Remove the underscores from type name 'W2MWPP_UI_Config'."

    Target : Word2MediaWiki__.Word2MediaWikiPlusPlus (TargetNamespace)

    Resolution : "Remove the underscores from namespace name 'Word2MediaWiki__.Word2MediaWikiPlusPlus'."

    Not that I am saying you need to, but if you'd like to take a closer look at the source code for this assembly, you can find it here:
    And one additional question: if these false positives from VS-generated code are benign, but it would still be *better* for source code manageability to eliminate the underscores, shouldn't we attack the source of the problem That is, would it be possible for the FxCop default rules library to warn about characters in source code that VS is likely to convert to underscores, and suggest that those characters not be used [Such a rule would sure help me figure out where in the source code Visual Studio is finding the characters that it's converting over to underscore characters. I've scoured my code and I can't find 'em, so I'm stumped.]


  • sryan

    More specifically, FxCop complains about underscores in my Namespace and in my form, e.g. F_Form

    /M


  • Leyan

    Am I understanding you correctly, David "feel un-.NET-like" Is this the only or the most egregious problem with code that's flagged by this rule And is it really OK to feel free to turn the rule off and use underscores under such circumstances

    If so, then why is this rule marked as follows:

    When to Suppress Warnings

    If I take your post at face value, then the "When to suppress warnings" notation is clearly in need of update, and I would urge your team very strongly to add some of this kind of perspective/context to the documentation around a rule like this. As it stands in FxCop/Code Analysis, rules like this seem very cut-and-dried, with little room for interpretation, and it seems that this is not in the best interests of the .NET community.

    Thank you in advance for considering this request/feedback.

    Cheers, Mike



  • ClaudiaHelpOnVSTO

    Yes you are interpreting the guidence correctly. Two points I'd like to meantion.

    1. If FxCop is firing on code that was automatically generated by VisualStudio then enabling the "suppress analysis results against generated code" option under the Spelling & Analysis tab should fix that or at least reduce the amount of generated code FxCop fires violations against.
    2. You are absolutely correct, you should only care about the underscores on publicly visible code elements. However I believe the rule will only fire on publicly visible code elements unless you have the "Run all overridable rules against all targets" option checked.

    -Todd



  • DotNetFireball

    The only options to you (that I know of) would be to just disable that specific rule in your FxCop project.

  • Error:Identifiers should not contain underscores