How do I prevent StyleCop warning of a Hungarian notation when prefix is valid
Asked Answered
U

2

11

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)?

Uzziel answered 6/9, 2018 at 11:18 Comment(4)
Why is fx valid in your naming convention? It's better these days to spell it out, to make sure there is no confusion.Saavedra
Good question @Neil. In finance, foreign exchange is almost always referred to as FX.Uzziel
So use var exchangeRate = new FxRate();. It's clear, unambiguous and doesn't require any domain knowledge (good for onboarding new devs).Saavedra
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.Uzziel
U
0

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.

Uzziel answered 14/9, 2018 at 15:51 Comment(0)
C
3

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.

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:

StyleCop Configuration


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:

StyleCop - NugetPackage

The StyleCop team recommends to use the StyleCopAnalyzers over the StyleCop extension when using VisualStudio 2015 and later.

Canzone answered 10/9, 2018 at 11:8 Comment(0)
U
0

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.

Uzziel answered 14/9, 2018 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.