2

假设我在 Ruby on Rails 中有这样的设置:

class A < ActiveRecord::Base
after_create :perform_some_action
#...
private
def perform_some_action
    if some_condition_met?
        #take some action
    end
end

我应该在方法名称中添加一些东西perform_some_action,以表明它取决于条件吗?

4

1 回答 1

0

Definitely, yes, because your condition check is inside the method.

This way, when calling your method from outside, the code will look self-explanatory. If not, when you read your call it will give you the impression that take_some_action is performed whithout any condition.

However, if 37 conditions are required inside your method, perhaps it's the signal that this method should be split up in several smaller ones.

于 2013-08-03T15:40:49.747 回答