Col*_*ker -1 java naming-conventions local-variables
编写一个程序来解释文本文件中的一行.
想知道我是否应该将方法'parseWordData'中的局部变量命名scannedWord
为oppposed word
,因为word
它已经是一个类字段.
只要我宣布一个新变量而不是重新分配旧变量,一切都应该没问题......对吗?
public class WordData {
private String word;
private int index;
private LinkedList<Integer> list;
private boolean pathFound;
public WordData(String word, int index, LinkedList<Integer> list, boolean pathFound) {
this.word = word;
this.index = index;
this.list = list;
this.pathFound = pathFound;
}
public WordData parseWordData(String line){
Scanner scan = new Scanner(line);
int index = scan.nextInt();
String word = scan.next();
//precond and subGoal
LinkedList<Integer> list = new LinkedList<Integer>();
while(scan.hasNextInt()){
//add to LinkedList
}
return new WordData(word, index, list, false)
}
Run Code Online (Sandbox Code Playgroud)
不要担心逻辑,我只是想知道这样的命名是否会令人困惑或者是java中的禁忌
在Java中,标准参数的标准做法与字段相同
只要这些方法将字段设置为与参数具有相同的值.在这种情况下,你会看到像这样的行
this.word = word;
Run Code Online (Sandbox Code Playgroud)
在您的构造函数或您的setter方法中.
在所有其他情况下,您应避免使用与字段名称相同的参数名称或局部变量名称.这只会导致混乱.这是标准做法.
因此,在您的示例中,您应该使用scannedWord
或类似于从输入中扫描的单词.