英文:
Are methods instead of functions preferred in this case
问题
我有一整套类似这样的函数:
package rules;
import "project/some"
func frobDiscountLimit(frob some.Frob, maxDiscount float64) (RuleStatus, string)
func frobDiscountPercentageLimit(frob some.Frob, maxDiscountPercentage float64) (RuleStatus, string)
func frobUsageLimit(frob some.Frob, interval valueInterval) (RuleStatus, string)
func frobPermanentExpiryLimit(frob some.Frob) (RuleStatus, string)
func frobVoucherValidity(frob some.Frob, maxValidityDays uint) (RuleStatus, string)
用法示例:
package rules
import "project/some"
func doChecks(frob some.Frob) {
status, message := frobDiscountLimit(frob, 100)
if status != PASSED {
....
}
... 在这里评估所有剩余的规则 ...
}
我想知道是否有优势这样写:
func (f someFrob) discountLimit(maxDiscount float64) (RuleStatus, string)
...
这样写似乎更简洁,但我不确定哪种是正确的风格。因为我不认为这些验证方法是“对象的一部分”,它们不是some.Frob
对象的行为的一部分(不像是some.Frob
的属性的getter或setter)。
英文:
I have a whole set of functions that look like this:
package rules;
import "project/some"
func frobDiscountLimit(frob some.Frob, maxDiscount float64) (RuleStatus, string)
func frobDiscountPercentageLimit(frob some.Frob, maxDiscountPercentage float64) (RuleStatus, string)
func frobUsageLimit(frob some.Frob, interval valueInterval) (RuleStatus, string)
func frobPermanentExpiryLimit(frob some.Frob) (RuleStatus, string)
func frobVoucherValidity(frob some.Frob, maxValidityDays uint) (RuleStatus, string)
Usage example:
package rules
import "project/some"
func doChecks(frob some.Frob) {
status, message := frobDiscountLimit(frob, 100)
if status != PASSED {
....
}
... evaluate all remaining rules here ...
}
I wonder is there any advantage writing it like:
func (f someFrob) discountLimit(maxDiscount float64) (RuleStatus, string)
...
It seems shorter to write, but I'm unsure which is the correct style. Because I don't think of these validation methods as "part of the object", they are not part of the behaviour of the some.Frob
object (unlike, say, a getter or a setter for property of some.Frob
).
答案1
得分: 4
你可以这样做,避免反复显式传递 frob
,同时仍然能够避免将这些函数作为 frob
的方法。
type frobChecker struct {
frob some.Frob
}
func (fc frobChecker) discountLimit(maxDiscount float64) (RuleStatus, string)
func (fc frobChecker) discountPercentageLimit(maxDiscountPercentage float64) (RuleStatus, string)
func (fc frobChecker) usageLimit(interval valueInterval) (RuleStatus, string)
func (fc frobChecker) permanentExpiryLimit() (RuleStatus, string)
func (fc frobChecker) voucherValidity(maxValidityDays uint) (RuleStatus, string)
func doChecks(frob some.Frob) {
fc := frobChecker{frob}
status, message := fc.discountLimit(100)
if status != PASSED {
....
}
... 在这里评估所有剩余的规则 ...
}
以上代码示例中,定义了一个名为 frobChecker
的结构体,其中包含一个 frob
的成员变量。然后,通过为 frobChecker
结构体定义相应的方法,可以在不将这些函数作为 frob
的方法的情况下,对 frob
进行各种检查。在 doChecks
函数中,通过创建 frobChecker
的实例 fc
,并将 frob
作为参数传递给它,可以使用 fc
来调用各种检查函数。
英文:
You could do something like this to avoid having to explicitly pass frob
over and over, while still being able to avoid making those functions methods on frob
.
type frobChecker struct {
frob some.Frob
}
func (fc frobChecker) discountLimit(maxDiscount float64) (RuleStatus, string)
func (fc frobChecker) discountPercentageLimit(maxDiscountPercentage float64) (RuleStatus, string)
func (fc frobChecker) usageLimit(interval valueInterval) (RuleStatus, string)
func (fc frobChecker) permanentExpiryLimit() (RuleStatus, string)
func (fc frobChecker) voucherValidity(maxValidityDays uint) (RuleStatus, string)
func doChecks(frob some.Frob) {
fc := frobChcker{frob}
status, message := fc.discountLimit(100)
if status != PASSED {
....
}
... evaluate all remaining rules here ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论