问题
What is a commonly accepted approach in variable naming when dealing with variables that are of different type but describe the same thing?
private String contentFolder = "/tmp/";
private File contentDirectory = new File(contentFolder);
The above seems sloppy. But what is better?
private String contentDirectoryStr = "/tmp/";
private File contentDirectory = new File(contentDirectoryStr);
Looks just as bad.
What is a common convention you follow to describe same things of different type?
Thought you can of course get String
from File
, for the purposes of this question, assume you legitimately need both a String
and a File
in your class.
回答1:
My first thought would be to use the same name, but differentiate based on the type.
Example:
private String contentDirStr = "/tmp/";
private File contentDirFile = new File(contentDirStr);
Of course it would better to not have two objects representing the same thing, but if you really need to, that's how I would go about naming them.
来源:https://stackoverflow.com/questions/11659036/naming-variables-describing-the-same-thing-but-of-different-type