I'm having difficulties to decide how to name a File variable and a String variable, which both describe the same directory.
Basically I have a String describing the location of a directory containing testcases. With this String I want to create a File object. Something like this:
String testcaseDirectoryPathString;
File testcaseDirectoryPath = new File(testcaseDirectoryPathString);
What I want to achieve is the most readability for my code, by choosing good variable names.
I searched the internet, but I end up finding naming conventions in general and the advice that variable names should be 'speaking'.
My question are for a specific examples, but my problem is not only with String and File naming but with choosing good variable names in general.
So here are some questions:
Is it good practice to add the word 'String' to any String object?
Is it good practice to add the word 'Path' to any File object?
Is it a matter of personal preference and there are no good practices?
How do you name your variabls and why?
If there are any articles out there, I would appreciate you pointing me to them.
Rules to Declare a VariableThe first character must not be a digit. Blank spaces cannot be used in variable names. Java keywords cannot be used as variable names.
For variables, the Java naming convention is to always start with a lowercase letter and then capitalize the first letter of every subsequent word. Variables in Java are not allowed to contain white space, so variables made from compound words are to be written with a lower camel case syntax.
A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)
Variables of general types (primitive and composite data types) should be named logically. Variables of specialized types (extended data types) should have the same name as the type (which should have a logical name) but starting with a lower case character.
it's just my opinion:
No, it doesn't contribute. Just make the name longer without reason adding the type in the name.
Same as nº1. You don't need the type in the name of the variable.
Always you need to follow a good practice coding to make it legible, have this in mind:
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - John F. Woods.
And most important thing, try allways to follow the company naming standards, maybe they aren't the best but will help to make the code rideable.
I always try to name my variables with short names, specifying something important and following the lower camel case using english even working in a spanish company where is not mandatary. And as I said following the company naming standards.
String testcaseDirectoryPathString;
File testcaseDirectoryPath = new File(testcaseDirectoryPathString);
If I don't have another directory path maybe I use this:
String directoryPath;
File <<something that describe the result file>> = new File(directoryPath);
with <<something that describe the result file>>
I means something like prizeRepresentations
, annualSales
, etc...
OFF THE RECORD If you use eclipse you can see the plugins checkstyle and pmd, they will help you to have a clean and clear code and to get used to work this way.
Is it good practice to add the word 'String' to any String object?
No, it just repeats the type. Use names which express the meaning of variables.
Is it good practice to add the word 'Path' to any File object?
No, see above. Additionally it would produce confusion for any reader: Is the type of the variable java.io.File
or java.nio.Path
?
In your example I would change the following:
Rename testcaseDirectoryPath
to testcaseDirectory
Rename testcaseDirectoryPathString
to testcaseDirectoryName
General guide is:
Get guidance on https://softwareengineering.stackexchange.com/ there are numerous questions in this topic. Usually together with "good practice" and "naming conventions"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With