Naming convention for member variables with initials
Asked Answered
L

3

7

I was wondering how someone would name the member field that began with initials as opposed to words.

public class MyClass {

    // I know this is how it is for a member field with words.
    private String myString;

    // What about this String? It is representing the term "RV Scale" which is 
    // short for "Reclose Volts Scale."
    private String RVScale;

}

I know that I could just use private String recloseVoltsScale, but I'd rather keep it RVScale. If anyone knows if its supposed to be rVScale or RVScale or something else I'd appreciate the info.

EDIT :

Also, what about a member field like this...

private int RV;
Lycopodium answered 30/7, 2012 at 13:34 Comment(6)
I'd use rvScale.Nones
From personal experience, I wouldn't abbreviate 'RV' unless you're absolutely certain that every developer from here until end of life of your software knows what it stands for. Spelling it out improves readability and documentation.Droshky
I agree with @JoachimSauer. RVScale looks like a class.Mu
@jskiles1 Yeah I know that, but I'm translating someones code from C to Java, and would like to keep things as similar as possible to his old code. I may change it to the full words, but I also posted the question due to curiosity.Lycopodium
You could also use sRVS, where s stands for string.Cuneiform
I would use "rv", but I would explain its meaning in a javadocNotus
D
4

as per java naming convention member field should start with mixedcase so it should be rvScale and member field like RV should be like private int rv;

Using the right letter case is the key to following a naming convention:

Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage).

Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK).

CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).

Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).

Diversification answered 30/7, 2012 at 13:42 Comment(2)
@Lycopodium should be private int rv;Diversification
@Lycopodium In consonance to rvScale, rv seems ok IMHO.Scrounge
B
2

You should spell it out.

That being said, I would keep it rvScale.

Keep in mind that the only one that should NOT be used is rVScale since this can cause some confusion with introspection.

This case would generate getter and setter named getRVScale and setRVScale, but using bean introspection to find the property associated with these, would search for a property named RVScale, which doesn't exist. This is due to the fact that when searching for the corresponding property, the first letter after the get/set is lower cased, unless there are two adjacent letters in upper case (RV), in that case, the name is left as is, producing RVScale.

Like explained here:

Javabean convention - method naming for property gId

Borroff answered 30/7, 2012 at 13:49 Comment(0)
A
1

Do not use shortcuts unless you need to. Even if they are private (or maybe because they are), name variables clear enough. If potential code reader does not understand rv as shortcut of something like Reclose Volts, do not use shortcut. If you need provide hint it is shortcut actually, you are hinting he should lookup it somewhere what does that mean. Where will he find it? If it is in comment, you have wrong name of member, as you have to explain what does the name mean. Unless you application is all about Reclose Volts. I suggest to follow java conventions as they did have reason to appear.

Amil answered 30/7, 2012 at 13:51 Comment(2)
Thanks for your input. Just a suggestion, try framing your answers in a friendlier manner that will aid the OP and all future readers in understanding the question better. Clearly you understand the issue, but your way of presenting the answer leads me to not pick your answer as the answer. The other two answers posted on here are much more clear cut.Lycopodium
Thanks, I will try. I am not very skilled in language though. Maybe I am unfriendly as part of my person. I did not try to explain camelcase anyway, as i expected you heard about that already. Harmeet's reply is far better than mine anyway. Still I will try to advice more and lecture less in future.Amil

© 2022 - 2024 — McMap. All rights reserved.