命名变量描述相同的事物但类型不同 [英] Naming variables describing the same thing but of different type
问题描述
当处理类型不同但描述相同内容的变量时,变量命名中普遍接受的方法是什么?
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);
看上去也很糟糕.
用于描述不同类型的相同事物的通用约定是什么?
出于这个问题的目的,您当然可以从File
获取String
,并假设您在类中合法地同时需要String
和File
.
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.
推荐答案
我的第一个想法是使用相同的名称,但根据类型进行区分.
My first thought would be to use the same name, but differentiate based on the type.
示例:
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.
这篇关于命名变量描述相同的事物但类型不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!