有条件操作的方法的良好命名约定是什么? [英] What's a good naming convention for methods that take conditional action?

查看:92
本文介绍了有条件操作的方法的良好命名约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个方法,Foo().根据方法ShouldFooNow()的确定,在某些情况下Foo()是适当的.但是,在很多时候程序必须考虑此时Foo()是否合适.所以不用写:

Let's say I have a method, Foo(). There are only certain times when Foo() is appropriate, as determined by the method ShouldFooNow(). However, there are many times when the program must consider if Foo() is appropriate at this time. So instead of writing:

if ShouldFooNow():
   Foo()

在任何地方,我都将其变成一个函数:

everywhere, I just make that into one function:

def __name():
    if ShouldFooNow():
       Foo()

此方法的好名字是什么?我很难提出一个好的约定. IfNecessaryFoo()很尴尬,特别是如果Foo()具有较长的名称. DoFooIfShould()?更加尴尬.

What would be a good name for this method? I'm having a hard time coming up with a good convention. IfNecessaryFoo() is awkward, particularly if Foo() has a longer name. DoFooIfShould()? Even more awkward.

什么是更好的姓名样式?

What would be a better name style?

推荐答案

我认为您非常接近.将动作/意图放在方法名称的开头,以便于字母搜索.如果我正在写类似的东西,我会考虑

I think you're pretty close. Put the action/intent at the head of the method name, for easier alphabetic searching. If I were writing something like that, I'd consider

FooIfNecessary()
FooIfRequired()

例如,

ElevatePermissionsIfNecessary()

这篇关于有条件操作的方法的良好命名约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆