Simple yet reoccurring naming problem
-
22-07-2019 - |
Question
Let's say we have a method:
public String wishes(Date birthday) {
String birthayDateString = convertToString(birthay);
...
}
I wonder what's the best name to give to the string called now "birthayDateString". This string represents date converted to text. I can't name it "birthay" beause this name is alredy used. Does nameing it "birthdayString" or "birthdayDateString" violate some naming convention good practice rules?
Solution
That actually looks fine.
I personally would prefer birthdayStr
- shorter and to the point - making it both meaningful and yet concise.
Yielding:
public String wishes(Date birthday) {
String birthdayStr = convertToString(birthday);
// whatever
}
OTHER TIPS
No, I don't think you're violating any "best practice" standards. In cases like those, I often use variable names like birthdayText
or birthdayStr
.
In general, though, I try to make variable names as meaningful as possible—I don't just automatically tack on "Str" or "Text" if there's a naming collision. But in this case, it's just the string representation of the date, so I'd say it's meaningful enough.
Call it whatever you want.
The variable name will only be in scope for the current method block (which should be reasonably small if you have your code properly broken up/encapsulated).
Personally I'd go with birthdayStr
just for clarity in that particular method.]
I'd also Upper case those MethodNames to Wishes
and ConvertToString