Do、Run、Execute、Perform 动词
- 您使用哪些动词列表作为方法名称?你的个人或团队标准是什么?
- 我争论是否使用“Do”、“Run”、“Execute”、“Perform”,并且想知道是否不再推荐其中任何一个,或者是否有一些人们并不真正使用,我应该把它们划掉。基本上这些动词中的任何一个都意味着相同的事情......调用某个过程(方法调用)。这不属于 CRUD 范围。例如:
ExecutePayPalWorkflow();
也可以是这些名称中的任何一个:
DoPayPalWorkflow();
RunPayPalWorkflow();
PerformPayPalWorkflow();
或者这并不重要......因为这些动词中的任何一个都可以理解为“什么”显示了您的“PayPalWorkflow”后面的其他词的意图
是,命名是我非常认真对待的事情,并且在编码时确实思考了很多:
- 它使代码对其他开发人员可读
- 它使我的代码在几周后当我不记得时对我自己可读我编码的内容
这个讨论可以适用于任何语言。我只是将 C# 和 Java 两个主要标签放在这里,这足以让我获得一些可靠的答案或经验。
- What list of verbs are you using for method names? What's your personal or team standard?
- I debate whether to use Do vs. Run vs. Execute vs. Perform and am wondering if any of these are no longer recommended or some that people just don't really use and I should just scratch them. Basically any one of those verbs mean the same thing...to invoke some process (method call). This is outside of CRUDs. For example:
ExecutePayPalWorkflow();
that could be also any one of these names instead:
DoPayPalWorkflow();
RunPayPalWorkflow();
PerformPayPalWorkflow();
or does it not really matter...because any of those verbs pretty much are understandable as to "what" shows your intent by the other words that follow it "PayPalWorkflow"
Naming is something I take very seriously and really think about a lot while coding as:
- it makes code readable to other devs
- it makes my code readable to myself weeks later when I can't remember what I coded
This discussion can go for any language. I just put the two main tags C# and Java here which is good enough for me to get some solid answers or experiences.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的方法名称通常不应该以动词开头吗?就像您的示例一样,您可以仅使用动词
Pay
而不是Perform
+Payment
。编辑:
至于如何使用您列出的任何动词(执行、执行、运行、执行),我认为它们根本没有必要。它们不传达任何附加信息。您正在调用(或执行或执行)方法的事实是由您的语法暗示的。
如果您想向调用者表明您的方法可能会在操作完成之前返回,则 .NET 中有一个约定:Begin* 和 End*。
我能想到的唯一例外是如果您对任何通用流程有抽象。在这种情况下,您将被迫使用“执行”或“运行”等动词,因为您实际执行的操作尚未定义。
Shouldn't your method names usually be verbs to start with? Like your example, you could just use the verb
Pay
instead ofPerform
+Payment
.Edit:
As for how you might use any of the verbs you list (Execute, Do, Run, Perform), I don't think they are necessary at all. They do not convey any additional information. The fact that you are calling (or executing or doing or running or performing) a method is implied by your syntax.
If you'd like to indicate to the caller that your method may return before the action has completed, there is a convention for that in .NET: Begin* and End*.
The only exception I can think of is if you have an abstraction for any general process. In that case, you'll be forced to use a verb such as Execute or Run, since what you're actually doing isn't yet defined.
我发现抽象是比长方法名称更好的不言自明代码形式。
也许这样对您来说更好:
这个
PayPalWorkflow
到底是什么?目前对我来说没有任何意义...您可能想要使用此
payPal.startPaymenetProcess();
或者您想通过 1 个方法调用来启动和完成它?
您必须更好地描述和抽象您的系统,然后简单的名称就会出现。
让我们忘记你的例子......
这是我对 do/perform/run/execute 的想法
- 这是关于连续过程的。会持续下去的东西。可以在运行时进行配置等...
do
- 我不会使用do
这个词,因为在 Java(我的工作语言)中,它是由语言执行
- ...一项任务。简短、快速执行并重复执行
的东西 - 复杂事情的最后一步,在此之前需要进行大量准备工作I find abstraction the better form of self-explanatory code, than long method-names.
May be it's better for you to have something like this:
What is this
PayPalWorkflow
anyway? It doesn't mean anything to me at the moment...May be you want to
payPal.startPaymenetProcess();
with this oryou want to initiate and finish it with 1 method call ?
You have to describe and abstract your system better and then the simple names will follow.
Let's forget your example...
and here's my thoughts regarding
do/perform/run/execute
run
- this is about continuous process. Something that will last. Can be configured while running etc...do
- I won't use thedo
word because in Java (my working language) it's a command defined by the languageperform
- ...a task. Something that is short and executed fast and repeatedlyexecute
- the final step of something complex with a lot of preparations necessary before that看来您的命名约定缺乏抽象。如果某个东西是“可执行的”,那么它的方法应该是“execute()”,并且您应该使用类名来指示什么实现了“可执行”的行为。
是否使用“执行”、“运行”或“执行”完全取决于系统的上下文。如果您将这些视为需要运行的作业,请不要使用“执行”。如果它们是需要执行的任务,“运行”同样是一个糟糕的选择。重要的是所有这些方法应该一致(这是接口声明将强制执行的)。
It seems your naming convention suffers from lack of abstraction. If something is "executable" then it's method should be "execute()" and you should use the class name to indicate what implements the "executable" behavior.
Whether you use "execute", "run", or "perform" is entirely dependent on the context of the system. If you view these as jobs that need to be run, don't use "perform". If they are tasks that need executed, "run" is likewise a bad choice. The important thing is that all of these methods should be consistent (which is what the interface declaration will enforce).
我说以上都不是。不同的前缀会损害 IntelliSense。
I say none of the above. Varying prefixes hurt IntelliSense.
有时,您在 c#/java 之前使用的语言会影响您将使用的术语。例如,有很多 Delphi 开发人员更喜欢
PerformSomeTask()
相反,我看到了很多 VB(我猜?)的影响,倾向于将方法前缀为
RunSomething()
我个人避免任何类似的约定,因为您的代码将开始看起来相似。我在 LinkedIn 上收到一位同事的推荐,他提到我维护的一个项目有超过 2,000,000 行代码。我没有意识到这个项目有那么大,但我从该项目中学到的一件事是,你不希望你的代码看起来像这样:
它只会让一切变得如此反可读。命名约定应该与大量的现实盐结合起来。
我认为该方法的名称应该简单地反映它的作用。有一天它可能是
PerformSomeTask
,另一天它可能是DoImportantThing
。但拥有 20 多个Perform[YourMethodNameHere]
并不是很直观。Sometimes the language you used before c# / java affects the term you will use. For instance there's a lot of Delphi developers out there who will prefer
PerformSomeTask()
Conversely, I've seen a lot of VB (I guess?) influence, with a tendency to prefix methods as
RunSomething()
I personally avoid any kind of convention like that, because your code will start to look similar. In a recommendation I received from a work colleague on LinkedIn, he mentioned that one project I maintained had 2,000,000+ lines of code. I didn't realize the project was that big, but one thing I learned from that project was that you don't want your code to look like this:
It just makes everything so anti-readable. Naming conventions should be taken with a large dose of reality salt.
I think the name of the method should simply reflect what it does. One day it might be
PerformSomeTask
, another day it might beDoImportantThing
. But having 20+Perform[YourMethodNameHere]
is not very intuitive.抛开语义不谈,在 Java 中,
public void run()
几乎总是正确的。这是因为Runnable
,RunnableFuture
和RunnableScheduledFuture
接口,当您希望某些内容在Thread
上运行或安排它稍后在 执行器。Semantics aside, in Java,
public void run()
is almost always correct. This is because of theRunnable
,RunnableFuture
, andRunnableScheduledFuture
interfaces, which are used when you want something to run on aThread
or schedule it for later execution on an Executor.保持简单吧。首先,函数名称应该小写。然后,您需要
generateReport
,而不是像PeformReportGeneration
这样的东西。它更简洁地传达了相同的信息。在你的情况下,这更困难,因为
Workflow
并不是真正的动词......不如用PerformPayPalWorkflow
你只需执行run
,例如:如果你所拥有的只是一个
PayPal
对象,它的作用不仅仅是工作流程,不过……我不确定。我需要更多有关代码如何设置、本例中的工作流程实际是什么等信息。Ya keep it simple. First of all the function names should be lowercased. Then, nstead of something like
PeformReportGeneration
, you wantgenerateReport
. It conveys the same message more succinctly.In your case it's harder since
Workflow
isn't really a verb... how about instead ofPerformPayPalWorkflow
you just dorun
, e.g.:If all you have is a
PayPal
object that does more than just workflow, though... I'm not sure. I'd need more info about how the code is set up, what a workflow actually is in this case, etc.