Resharper Not Recognizing Acronym in List
Asked Answered
L

3

6

I am trying to clean up warnings R# 6.1 is generating for my classes and one of the issues ReSharper is reporting is that I have incorrect capitalization on a variable. For instance I have var RECDLeft = new RECD(); and it recommends that I change it to var recdLeft = new RECD() despite it being an acronymn defined in the list. I have manually added the RECD acronym to the list of acronyms because it wasn't asking me to add it in the quick fix menu. I have noticed that if I call the variable `var aRECDLeft' it recognizes the acronym properly. Is there a reason acronyms are not recognized at the beginning of a variable name? And is there a way to make R# recognize this usage besides moving the acronym to the second word?

Thanks, Mark Smith

Lundin answered 9/3, 2012 at 14:51 Comment(2)
I suggest asking on the Resharper forum. This isn't a programming question this is a Resharper question.Brunobruns
@Ramhound - I've had better luck on here than on R#'s forums. So although it isn't exactly code related I figured a good number of people on here use the tool. If nobody on here has any info I'll switch to ReSharper's forums.Lundin
K
4

In answer to your first question, I guess R# is trying to conform to Microsoft's C# conventions for acronyms:

Capitalization Rules for Acronyms

Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.

A property named DBRate is an example of a short acronym (DB) used as the first word of a Pascal-cased identifier. A parameter named ioChannel is an example of a short acronym (IO) used as the first word of a camel-cased identifier.

Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier.

A class named XmlWriter is an example of a long acronym used as the first word of a Pascal-cased identifier. A parameter named htmlReader is an example of a long acronym used as the first word of a camel-cased identifier.

Do not capitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier.

A parameter named xmlStream is an example of a long acronym (xml) used as the first word of a camel-cased identifier. A parameter named dbServerName is an example of a short acronym (db) used as the first word of a camel-cased identifier.

Kimbrough answered 9/3, 2012 at 16:51 Comment(5)
This isn't correct. You can add full-uppercase acronyms and R# will stop flagging names with those in them. For example, if you add XML, it will not flag a method like ParseXML() as in error, even though it violates the MS guidelines.Berhley
@Berhley - R# does allow you to add the acronyms however, parseXML is valid, while XMLParse is invalid according to R# with my settings. The Microsoft convention is what we refer to as a basis for our standards so I'll follow the convention Rich linked which will make R# happy.Lundin
@MarkSmith: fair enough if you are following MS standards. However, that's not why R# was behaving strangely and thus is not really the answer to your question.Berhley
I guess I should have phrased my question differently then. A lot of these sorts of issues I either want to know a workaround or why I shouldn't apply a work around. My end goal is not to fix R# but to improve my code. I marked Rich's answer as correct because it's fulfilled my unspoken requirement and had a good amount of detail about the best practice involved.Lundin
I was thinking that the incorrect capitalization reported by R# referred to starting a variable with a capital letter. If you set R# to camel-cased variables then you can't start with a capital letter, even if it's an acronym. I didn't make this plain in my answer.Kimbrough
H
3

As siride correctly points out, this is a special naming case that requires introducing a special naming rule for local variables that would be checked in addition to your default rule. Here's what you should do:

  1. Go to ReSharper > Options > Code Editing > C# > C# Naming Style
  2. Select Local variables, and click Edit.
  3. In the Edit Rule Settings dialog box, what you see by default is one single rule, lowerCamelCase. You could have changed it to another style though.
  4. What you need to do is add another naming rule by clicking Add and set both name prefix (RECD in your case) and style to UpperCamelCase. If you only add the prefix, ReSharper will keep bitching (and giving some funny renaming suggestions) because you also spell "Left" instead of "left".

If you have other similar abbreviations, you should add an additional local var naming rule for each of them (the entire list of known abbreviations is available at C# Naming Style > Advanced settings > Abbreviations as plain text.) This is not very handy to do but it's not that you have a simple naming standard either )

Hope this helps.

Hallee answered 10/3, 2012 at 6:40 Comment(2)
I was hoping for a rule that just allows acronyms to be treated differently without adding a new rule for each attribute, but then again we don't always get what we want. Since I work on a huge project in an industry littered with acronyms, individual rule for each acronym would work, it's way more tedious than me just giving up on what I thought was correct style and follow the Microsoft conventions. Thanks for the detail, it's really helpful for the case where someone needs it to work.Lundin
Ugh, you're gonna steal my points ;). Definitely a very good answer, though.Berhley
B
2

I'm guessing the problem is that although the acronym is added, you are still violating the rule that variable names start with lowercase. You'd need to add a special rule for variable names that allows them to begin with that acronym. This is not the same as adding an acronym to the list.

Berhley answered 9/3, 2012 at 17:9 Comment(1)
Looks like this is true. Will elaborate on that in a separate answer.Hallee

© 2022 - 2024 — McMap. All rights reserved.