gpt4 book ai didi

ruby-on-rails - ruby/rails bool 方法命名约定

转载 作者:数据小太阳 更新时间:2023-10-29 07:47:10 26 4
gpt4 key购买 nike

我有一个关于 ruby​​/rails 方法命名约定或良好实践的简短问题。考虑以下方法:

# some methods performing some sort of 'action'
def action; end
def action!; end

# some methods checking if performing 'action' is permitted
def action?; end
def can_action?; end
def action_allowed?; end

所以我想知道,这三种与号方法中的哪一种是请求权限的“最佳”方式。我会以某种方式使用第一个,但在某些情况下,我认为这可能与 has_performed_action? 的含义混淆。所以第二种方法可能会更清楚,但也有点冗长。第三个实际上只是为了完整性。我不太喜欢那个。

那么,是否有任何共同商定的良好做法?

最佳答案

我认为这取决于您要执行的操作和要检查的操作。我的目标是可读性和最少的混淆:

self.run
self.can_run? # can this object run?
self.running_allowed? # is running allowed here or by this user?

self.redeem!
self.redeemable? # is this object currently redeemable?

self.copy_to_clipboard
self.copy_to_dashboard
self.copyable? or self.can_be_copied? #can this object be copied
self.copy_allowed?(:clipboard) # is copying to the clipboard allowed by me?

关于ruby-on-rails - ruby/rails bool 方法命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4951597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com