[ACCEPTED]-How to fix violation of StyleCop SA1305 (Hungarian)-hungarian-notation

Accepted answer
Score: 12

You can also suppress stylecop on a case-by-case 3 basis. e.g.

[System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.StyleCop.CSharp.NamingRules",
    "SA1305:FieldNamesMustNotUseHungarianNotation",
    Justification = "Using Win32 naming for consistency.")]
IntPtr hFile;

This might not be an attractive 2 option if you have numerous offending names, but 1 for one or two, it's generally fine.

Score: 8

You can also use the Settings.StyleCop in the package files 4 to configure the settings.

You can suppress 3 specific words by adding below code to the 2 Settings.StyleCop file:

<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
  <AnalyzerSettings>
    <CollectionProperty Name="Hungarian">
      <Value>as</Value>
      <Value>do</Value>
      <Value>id</Value>
      <Value>if</Value>
      <Value>in</Value>
      <Value>ip</Value>
      <Value>is</Value>
      <Value>mx</Value>
      <Value>my</Value>
      <Value>no</Value>
      <Value>on</Value>
      <Value>to</Value>
      <Value>ui</Value>
      <Value>vs</Value>
      <Value>x</Value>
      <Value>y</Value>
      <Value>z</Value>
      <Value>iOS</Value>
      <Value>IOS</Value>
    </CollectionProperty>
  </AnalyzerSettings>
</Analyzer>

You can suppress the Hungarain Rule itself 1 by adding the following to the Settings.StyleCop file

<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
  <Rules>
   <Rule Name="FieldNamesMustNotUseHungarianNotation">
    <RuleSettings>
     <BooleanProperty Name="Enabled">
        False
     </BooleanProperty>
    </RuleSettings>
   </Rule>
 </Rules>
</Analyzer>
Score: 7

You could take a look at StyleCop+. It contains flexible 21 naming rules that will allow you to force 20 all private fields be named starting with 19 "m_" (or whatever you wish) instead 18 of disabling name checking (like you did).

Regarding 17 "d3dDevice" - it's a very interesting 16 case. Logically, it splits to the following 15 words - { "d", "3", "d", "Device" } or 14 { "d3", "d", "Device" }. And 13 the second "d" seems not to follow 12 "camelNotation".

But, I strongly 11 believe that static analysis (particularly 10 naming) should be flexible enough to satisfy 9 user needs. Currently StyleCop+ can support 8 your case in the following way - for example, you 7 can add "exception" (as many as 6 you want) to naming template for private 5 fields, so that it will look like:

m_$(aaBb)
m_d3d$(AaBb)

This 4 is more likely to be workaround, but I will 3 think about your "d3d" case - and 2 maybe StyleCop+ will support something like 1 this.

Thank you for the interesting example!

Score: 0

Adding suppression attribute should be done 3 on top of all methods which will take time 2 and a long process.

If you would like to 1 remove this rule from your project try this

  • Right click on your project
  • Select Stylecop Settings
  • Find SA1305
  • Uncheck the rule from result set
  • Click Apply - OK
  • Rerun style cop rules again.

More Related questions