Naming variables describing the same thing but of different type

半城伤御伤魂 提交于 2019-12-11 03:05:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!