gpt4 book ai didi

ruby - 这些方法命名方法中哪种更好,为什么?

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

在我的应用中,我们的用户可以相互执行操作 - 比如在 Facebook 上

我刚刚正在编写这些方法,我不太确定该采用哪种方法。我的意思是,我知道它们都是可以接受的,但是否有更惯用的方法?

选项1

if @current_user.may_poke?(@other_user)
@current_user.poke!(@other_user)
end

选项 2

if @current_user.may_poke?(@other_user)
@other_user.poke!(@current_user)
end

第一个选项用英语读起来更好,几乎可以完美地作为一个句子。第二个选项在方法命名方面更有意义,“poke”是在 @other_user 上执行的方法。 @current_user 只是提供额外信息的参数 - 谁做了戳。

最佳答案

我会坚持使用选项 1。如果您遵循条件逻辑,那么您是在问“如果 current_user 可以戳 other_user,则让 current_user 戳 other_user”。从这些方面考虑,选项 2 没有多大意义。

此外,Ruby 的作者 matz 指出“bang [感叹号] 符号意味着“bang 版本比非 bang 版本更危险;小心轻放”。” (http://www.ruby-forum.com/topic/176830#773946)。我可能只使用 poke 作为该方法名称而不是 poke!

关于ruby - 这些方法命名方法中哪种更好,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836769/

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