Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: variable names for a directory path, naming variables in general [closed]

Tags:

java

string

file

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:

  1. Is it good practice to add the word 'String' to any String object?

  2. Is it good practice to add the word 'Path' to any File object?

  3. Is it a matter of personal preference and there are no good practices?

  4. How do you name your variabls and why?

If there are any articles out there, I would appreciate you pointing me to them.

like image 674
tool Avatar asked Jan 27 '16 09:01

tool


People also ask

What are the 3 rules for naming variables in Java?

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.

What is the correct naming convention for variables in Java?

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.

What are the 4 rules for variable names?

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)

What is a good naming convention for 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.


3 Answers

it's just my opinion:

  1. Is it good practice to add the word 'String' to any String object?

No, it doesn't contribute. Just make the name longer without reason adding the type in the name.

  1. Is it good practice to add the word 'Path' to any File object?

Same as nº1. You don't need the type in the name of the variable.

  1. Is it a matter of personal preference and there are no good practices?

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.

  1. How do you name your variabls and why?

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.

like image 174
Arturo Avatar answered Sep 22 '22 10:09

Arturo


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

like image 40
wero Avatar answered Sep 18 '22 10:09

wero


General guide is:

  • be specific, if you read your field/variable name, get a clue what is it for, but don't overdo it. Always check it out with an eye of an outsider. Would you like to see and work with a field named businessRegulationSecondServiceFactoryInstanceFromASingleton? On the other end, never use one character field, variable, only if it is a very basic index for a loop.
  • don't need to repeat the String since all IDE will help you out in determining the type.
  • avoid negations if it is not neccessary, like 'boolean notDisabled' use 'boolean enabled'.
  • keep your names tidy, and use recognizable patterns through your code, if anybody reviews your java files, at the second or third class, that person should get a picture of your programming style, what (s)he should expect on the other parts of your code.

Get guidance on https://softwareengineering.stackexchange.com/ there are numerous questions in this topic. Usually together with "good practice" and "naming conventions"

like image 40
CsBalazsHungary Avatar answered Sep 20 '22 10:09

CsBalazsHungary