How do I prevent StyleCop warning of a Hungarian notation when prefix is valid
How do I prevent StyleCop warning of a Hungarian notation when prefix is valid
I have the following code:
var fxRate = new FxRate();
which is giving me the following StyleCop ReSharper warning:
The variable name 'fxRate' begins with a prefix that looks like
Hungarian notation.
I have tried copying the Settings.StyleCop file to my solution folder and adding an entry for fx:
<Analyzers>
<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
<AnalyzerSettings>
<CollectionProperty Name="Hungarian">
...
<Value>fx</Value>
...
I've restarted VS but I still get the same warning.
I am using the StyleCop ReSharper extension in VS2017.
How do I ensure 'fx' is a valid prefix in the solution (for all team members)?
fx
Good question @Neil. In finance, foreign exchange is almost always referred to as FX.
– openshac
Sep 6 '18 at 11:41
So use
var exchangeRate = new FxRate();
. It's clear, unambiguous and doesn't require any domain knowledge (good for onboarding new devs).– Neil
Sep 6 '18 at 11:43
var exchangeRate = new FxRate();
StyleCop allows for prefixes to be declared as legal (and not Hungarian). I would like to know how to do this for any prefix that team agrees is valid. Of course, on a case by case by case basis, there can be a discussion on whether the prefix is valid. However, I would just like to know how to implement a solution.
– openshac
Sep 6 '18 at 13:28
2 Answers
2
I am using VisualStudio 2017 with ReSharper 2018.2 and the corresponding StyleCop by JetBrains extension (Version 2018.2.0 - StyleCop.ReSharper.dll 5.0.6329.1 )
In our projects I have added the Settings.StyleCop
file in the solution folder next to the solution file.
To test your prefix I have added fx
to my settings file and it worked out of the box.
Settings.StyleCop
fx
My file contains the following analyzer rule.
<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
<Rules>
<Rule Name="FieldNamesMustNotUseHungarianNotation">
<RuleSettings>
<BooleanProperty Name="Enabled">True</BooleanProperty>
</RuleSettings>
</Rule>
</Rules>
<AnalyzerSettings>
<CollectionProperty Name="Hungarian">
...
<Value>fx</Value>
..
</CollectionProperty>
</AnalyzerSettings>
And my Resharper configuration looks like this:
How do I ensure 'fx' is a valid prefix in the solution for all team members)?
In our projects we always check in the StyleCop settings file, so we ensure that the all members use the right one and we can keep it up-to-date for all.
In addition to the ReSharper Plugin you can also use the StyleCop Package found in the NuGet store and add it to your solution:
The StyleCop team recommends to use the StyleCopAnalyzers over the StyleCop extension when using VisualStudio 2015 and later.
I updated ReSharper to 2018.2.1 and the now the values in the the file:
<CollectionProperty Name="Hungarian">
...
<Value>fx</Value>
...
are getting successfully recognised. This makes the warning go away.
I'm not sure if it was the update itself or something like a cache being cleared.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why is
fx
valid in your naming convention? It's better these days to spell it out, to make sure there is no confusion.– Neil
Sep 6 '18 at 11:36