Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
干净的代码:使用前缀get作为java函数名_Java_Naming Conventions_Clean Architecture - Fatal编程技术网

干净的代码:使用前缀get作为java函数名

干净的代码:使用前缀get作为java函数名,java,naming-conventions,clean-architecture,Java,Naming Conventions,Clean Architecture,关于干净的代码,在java中使用前缀“get”作为函数名是否不好,因为它可能与JavaBean混淆 示例:carService.getCarDetails()、customerService.getCustomers()…如果它做得更多,您可能想说“calclulateBalance()”或“findCustomerForID(String userID)”,但可以使用“get”。get和Setter来自计算机科学,在Java生态系统中随着类的出现而流行。当您有一个应用程序时,通常会有get方法

关于干净的代码,在java中使用前缀“get”作为函数名是否不好,因为它可能与JavaBean混淆


示例:carService.getCarDetails()、customerService.getCustomers()…

如果它做得更多,您可能想说“calclulateBalance()”或“findCustomerForID(String userID)”,但可以使用“get”。

get和Setter来自计算机科学,在Java生态系统中随着类的出现而流行。当您有一个应用程序时,通常会有get方法来读取值,而后面没有任何业务逻辑,并且一些框架强制您使用get/set方法

但是,当您开始使用领域专家使用的相同语言()来建模问题时,您将以对您的问题领域有意义并提高可读性的方法名称结束


在您的情况下,
getCarDetails()
当它对您的业务模型有意义时,它就可以了

你的意思是“即使实现不是简单的
返回someField
即字段的getter的实现,我还是应该说
getXXX
”?是的,也许只对getter字段使用getXXX更好没有这样的书面规则。遵循组织制定的标准。虽然widly get and set前缀用于getter和setter,但没有人阻止您在其他任何地方使用它。