How to fix StyleCop SA1305 violation (Hungarian)

My code contains a variable called "m_d3dDevice".

StyleCop complains about this name:

SA1305: The variable name 'm_d3dDevice' begins with a prefix that is similar to the Hungarian notation. Remove the prefix or add it to the list of allowed prefixes.

(Note. I manually disabled SA1308 ("m_"), one of the few rules that I am willing to disregard.)

I cannot resolve "d3d" as an exception on a tab in Hungary, since it allows only 1 or 2 char prefixes, and resolving "d3" did not help. I tried everything I could think of to add "d3d" to my CustomDictionary file (and in any case, the documents imply that CustomDict is not used for rule 1305).

Any suggestions for creating StyleCop to do this? Now the question of pride is not to have for variable F2.

+8
stylecop hungarian-notation
source share
4 answers

You can watch StyleCop + . It contains flexible naming rules that allow you to force assignment of all private fields starting with "m_" (or whatever you wish) instead of disabling name checking (like you).

Regarding "d3dDevice", this is a very interesting case. Logically, it is divided into the following words - {"d", "3", "d", "Device"} or {"d3", "d", "Device"}. And the second "d" does not seem to follow "camelNotation".

But I strongly believe that static analysis (in particular, naming) should be flexible enough to meet the needs of users. StyleCop + can currently support your case as follows - for example, you can add an β€œexception” (as many as you like) to the naming pattern for private fields so that it looks like this:

t _ $ (AABB)
m_d3d $ (AABB)

This is most likely a workaround, but I'll think about your "d3d" case, and maybe StyleCop + will support something like this.

Thanks for the interesting example!

+6
source share

You can also suppress a stylish style in each case. eg.

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

This might be an unattractive option if you have numerous abusive names, but for one or two in general this is normal.

+11
source share

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

You can suppress certain words by adding the code below to the 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 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> 
+1
source share

Adding a suppression attribute must be performed on top of all methods that take time and a long process.

If you want to remove this rule from your project, try this

  • Right click on your project
  • Select Stylecop Options
  • Find SA1305
  • Uncheck the result set
  • Click "Apply" - "OK"
  • The rules of re-repeat style again.
0
source share

All Articles