我应该在方法名称中使用“get”前缀吗?
我有一个用作数据存储库的抽象类。
public abstract class AbstractDataSource {
public abstract DataRow getDataRow(Key key); //or just dataRow(Key key)???
}
public class CSVDataSource extends AbstractDataSource {
@Override
public DataRow getDataRow(Key key) { //or just dataRow(Key key)??
//fetch row from file and return dataRow
//....
return dataRow;
}
}
更具体的类(即该类的子类)实现针对不同数据源的方法,例如CSV、ARFF和其他格式。
我想知道“get”关键字在这里是否合适,因为数据检索不仅返回变量,而且还可能需要文件系统访问或其他任何内容。
那么我应该使用“get”前缀吗?
谢谢
I have this abstract class that is used as data repository.
public abstract class AbstractDataSource {
public abstract DataRow getDataRow(Key key); //or just dataRow(Key key)???
}
public class CSVDataSource extends AbstractDataSource {
@Override
public DataRow getDataRow(Key key) { //or just dataRow(Key key)??
//fetch row from file and return dataRow
//....
return dataRow;
}
}
More specific classes (i.e. subclasses of this class) implement methods for different data source, e.g. CSV, ARFF and other formats.
I was wondering if the "get"-Keyword is appropriate here, as the data retrieval is not just returning a variable, but could also require filesystem access or whatsoever.
So should I use the "get"-prefix or not?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意,当背后有真正的逻辑时,使用
get
关键字有时看起来不直观。在您的情况下,lookup
或find
前缀可能是很好的替代品。正如@Thomas 所建议的,还有retrieve
。这个问题乍一看似乎偏离主题。但至少对我来说,命名是良好应用程序设计不可或缺的一部分。
I agree that using the
get
keyword can sometimes seem non-intuitive when there's real logic behind. In your caselookup
orfind
prefixes could be good substitues. There's alsoretrieve
as suggested by @Thomas.This question might seem off topic at first. But naming is, at least to me, an integral part of good application design.
你是否希望这个方法成为一个bean getter,这样你就可以将它与< a href="http://static.springsource.org/spring/docs/1.2.9/api/org/springframework/beans/BeanUtils.html" rel="nofollow">BeanUtils?鉴于它正在执行大量 I/O 和一些逻辑,可能不会,所以我会以不同的方式命名它。
Do you want to this method to be a bean getter, so you can use it with things like BeanUtils? Given that it is doing heavy I/O and some logic, probably not, so I would name it differently.