ruby / rails布尔方法命名约定。

时间:2023-01-18 21:22:01

I have a short question on ruby / rails method naming conventions or good practice. Consider the following methods:

我有一个关于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

So I wonder, which of the three ampersand-methods would be the "best" way to ask for permissions. I would go with the first one somehow, but in some cases I think this might be confused with meaning has_performed_action?. So the second approach might make that clearer but is also a bit more verbose. The third one is actually just for completeness. I don't really like that one.

所以我想知道,在这三个&方法中,哪一个是请求权限的“最佳”方式。我可能会选择第一个,但在某些情况下,我认为这可能与has_穿孔_action相混淆。所以第二种方法可能更清楚,但也有点啰嗦。第三个是为了完整性。我不太喜欢那个。

So are there any commonly agreed-on good practices for that?

那么有什么公认的好做法吗?

4 个解决方案

#1


4  

I think that depends on the action you want to perform and the action you are checking for. I would aim for readability and least amount of confusion:

我认为这取决于你想要执行的动作和你要检查的动作。我的目标是可读性和最少的困惑:

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?

#2


2  

I wouldn't use action?, because typically, single-word question-mark methods are used to indicate the presence (or absence) of a value. Rails lets you write English-like code, so pick a method name that makes the code the most readable.

我不会用行动?,因为通常使用单字问号方法来指示值的存在(或不存在)。Rails允许您编写类似于英语的代码,因此请选择一个方法名,使代码具有最好的可读性。

perform_action!("update") if action_allowed?("update")

Seems perfectly readable to me.

对我来说是完全可读的。

#3


1  

I'd go up one level and rethink the original method names. If the methods perform an action, then the role they play is that of a verb, not a noun:

我将提升一个级别,重新考虑最初的方法名。如果这些方法执行一个动作,那么它们扮演的角色是动词,而不是名词:

# some methods performing some sort of 'action'
def act
def act!

This has the handy side effect of a more natural-sounding answer to your question:

这有一个更自然地回答你的问题的便利的副作用:

# method checking if 'action' is permitted
def can_act?

...as well as some other obvious variants:

…以及其他一些明显的变体:

# methods checking if 'action' was performed
def acted?
def did_act?

# method checking if 'action' is called for by other circumstances
def should_act?

# method putting 'action' into a work queue
def act_later(delay_seconds=0)

Et cetera.

等等。

#4


0  

def can_action?; end

#1


4  

I think that depends on the action you want to perform and the action you are checking for. I would aim for readability and least amount of confusion:

我认为这取决于你想要执行的动作和你要检查的动作。我的目标是可读性和最少的困惑:

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?

#2


2  

I wouldn't use action?, because typically, single-word question-mark methods are used to indicate the presence (or absence) of a value. Rails lets you write English-like code, so pick a method name that makes the code the most readable.

我不会用行动?,因为通常使用单字问号方法来指示值的存在(或不存在)。Rails允许您编写类似于英语的代码,因此请选择一个方法名,使代码具有最好的可读性。

perform_action!("update") if action_allowed?("update")

Seems perfectly readable to me.

对我来说是完全可读的。

#3


1  

I'd go up one level and rethink the original method names. If the methods perform an action, then the role they play is that of a verb, not a noun:

我将提升一个级别,重新考虑最初的方法名。如果这些方法执行一个动作,那么它们扮演的角色是动词,而不是名词:

# some methods performing some sort of 'action'
def act
def act!

This has the handy side effect of a more natural-sounding answer to your question:

这有一个更自然地回答你的问题的便利的副作用:

# method checking if 'action' is permitted
def can_act?

...as well as some other obvious variants:

…以及其他一些明显的变体:

# methods checking if 'action' was performed
def acted?
def did_act?

# method checking if 'action' is called for by other circumstances
def should_act?

# method putting 'action' into a work queue
def act_later(delay_seconds=0)

Et cetera.

等等。

#4


0  

def can_action?; end