views:

354

answers:

7
  • 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.

+1  A: 

I say none of the above. Varying prefixes hurt IntelliSense.

Stephen Cleary
so you're saying there's no consistency in what you will be looking for when Intellisense pops up cause you're going to have DoThis ExecuteThis, etc. and it's going to be a very unorganized listing in Intellisense?
CoffeeAddict
So then what do you call methods that start/invoke a workflow or main process?
CoffeeAddict
Asynchronous methods have a single convention: Begin* and End*. Other methods are synchronous. If anything, I'd vary the suffixes (e.g., *Async, *Task).
Stephen Cleary
Not so much with Visual Studio 2010. Intellisense now does keyword matching, not just prefix.
spoulson
@spoulson: That is true; and I believe ReSharper and other plugins did the same for earlier VS versions.
Stephen Cleary
+2  A: 
payPalWorkflow.execute()
payPalWorkflow.run()
payPalWorkflow.perform()

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).

Edwin Buck
you should use the class name to indicate what implements the "executable" behavior. (I've got the namespace included already)
CoffeeAddict
then what's the diff between run and execute? You see to me the are the same concept
CoffeeAddict
And why do you think Run is a bad choice?
CoffeeAddict
What if PayPalWorkFlow had several types of processes you can execute, run or perform? You see an Interface is not the solution here
CoffeeAddict
If you're including the namespace, perhaps you're hiding the appropriate part of the name which gives the execute context, and now you're trying to recover the missing namespace hint by horribly mangling the method name? Also, typically I use run for something when I want to emphasize its time to complete, but I use execute when I only want to emphasize its launching.
Edwin Buck
I don't know, to me, why have a Class.Member when Class is just to me clutter. If you want to know where it's stemming from, just mouse over the dang thing. I can't stand having a thousand ClassName.MethodName all over....ClassName becomes burdensome clutter in your code when you don't need to resolve naming conflicts....which hopefully there shouldn't be many if you developed your architecture well and named stuff with a good naming standard.
CoffeeAddict
+8  A: 

Shouldn't your method names usually be verbs to start with? Like your example, you could just use the verb Pay instead of Perform + 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.

Michael Petito
PayPal is the kind of workflow. This is the PayPal API I'm using to make calls against...my custom logic.
CoffeeAddict
Also just using PayWorkflow() does not tell me that you're intent is to start some workflow. It's like saying CarString() instead of GetCarString(). just CarString tells me nothing about what I'm about to call....and if I would even get anything back without first looking at the signature's return value. It sounds like an incomplete name to me.
CoffeeAddict
Exactly, PayPal is a kind of *Payment* workflow. I might expect your abstract Payment workflow to define a method Pay?
Michael Petito
`CarString` does not start with a verb, hence the ambiguity. `GetCarString` does start with a verb, much like `Pay`.
Michael Petito
Ok, then talking verbs, use Start vs. Perform....same type of question still
CoffeeAddict
I like the Begin and End. Thanks for the feedback.
CoffeeAddict
Sure. Just be aware that if you use Begin and End and show your code to a .NET developer, they might at first assume your methods will have signatures compatible with `System.IAsyncResult`.
Michael Petito
Mike thanks for being a regular human being. Not a cry baby.
CoffeeAddict
+1  A: 

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:

PerformTransaction()    
PerformSubtransaction()
PerformDatabaseConnection()
PerformValidation()
PerformAuthorization()
PeformReportGeneration()
PerformReportGenerationPostTasks()
PerformUserLogOutFinalizationTasks()
PerformDataExport()
PerformDataImport()
// .... and on and on and on...

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 be DoImportantThing. But having 20+ Perform[YourMethodNameHere] is not very intuitive.

code4life
nice. But what you're starting the Main workflow for PayPal or something when a user gets back to your page and it's pretty generic. The Main workflow kicks off just maybe a couple initial method calls thereafter that really start the workflow. In my case, Rather than putting 3-4 method calls in my C# ASP.NET Page_Load (code-behind), I wrap those 3-4 method calls into a method called RunPayPalWorkflow which really wraps those 4 calls that initiates the process for the workflow nicely into a one line call in my Page_Load now instead of cluttering up the Page_Load with too much logic.
CoffeeAddict
@coffeeaddict, that's what I mean, we want to avoid these generic-sounding method names, it just makes code readability hit zero. Regarding those 3-4 method calls, I'm on the same page with you - just don't prefix them with Run...
code4life
@coffeeaddict: If you're going to devise a method to wrap calls to your workflow for PayPal, you should consider if that really ought to be part of your PayPal workflow interface. What if the logic is different between clients of your workflow? You might be needlessly introducing a coupling between your workflow and a particular page.
Michael Petito
@code4life: I wonder why the author of your example code didn't simply use: Connect(), Validate(), Authorize(), ... etc
Michael Petito
From some of the examples:PerformSubtransaction = Substract, PerformDatabaseConnection = ConnectToDatabase, PerformValidation = Validate, PerformAuthorization = Authorize, PerformReportGeneration = GenerateReports, PerformDataExport = ExportData, PerformDataImport = ImportData. I think this sort of names are clearer.
bloparod
@Michael? The code I put up are all examples, not actual code.. Ugh, imagine if your project only had methods that began with Perform... :)
code4life
@bloparod: LOL! Do you mean clearer with the prefix **Perform**, or without? Well, I guess it's miles better than PerformTask1, PerformTask2, PeformTask3, et al...
code4life
well, I guess Authorize, ExportData, etc. would not tell me context even if the class name helps. What if you have multiple methods to export data of different types? You might be able to overload but then ExportData will only tell you what type it is by its signature. You could have multiple kinds of export methods in a class...and have a couple Export methods with the same type in the signature but since you can't overload it, you're now going to have to give it a more descriptive name anyway.
CoffeeAddict
I guess what I'm saying is you can't always depend on the class name to give context to each and every method ever created in that class. Naming the method other than something generic (sometimes you want generic but I am not talking about those scenarios) gives your user of that method more context and they can pick up what's going on quicker than debugging through a ton of levels of code. Yes classes help but they are no the end all for ensuring readability. "Export"...Export What? What if the class name isn't what you're exporting? Create another class or rename
CoffeeAddict
@coffeeaddict: Yes, you are absolutely correct. A good example is the verb `Get`. Get what? Well, GetName, or GetID. Sometimes you must combine a verb with a noun when the action of the verb alone is unclear...
Michael Petito
@coffeeaddict, I agree with your point about Authorize, ExportData, etc (man I wish I thought up some more inspired names). My point is that attaching the same prefix to all these methods won't make the issue of relevant naming any better. But you're right, I these method names definitely need to be more descriptive!
code4life
@code4life: Without the Perform, obviously.
bloparod
+1  A: 

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:

PaymentMethod payPal = new PaymentMethod("paypal");
payPal.pay();

What is this PayPalWorkflow anyway? It doesn't mean anything to me at the moment... May be you want to payPal.startPaymenetProcess(); with this or

you 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 the do word because in Java (my working language) it's a command defined by the language

perform - ...a task. Something that is short and executed fast and repeatedly

execute - the final step of something complex with a lot of preparations necessary before that

Leni Kirilov
A: 

Semantics aside, in Java, public void run() is almost always correct. This is because of the Runnable, RunnableFuture, and RunnableScheduledFuture interfaces, which are used when you want something to run on a Thread or schedule it for later execution on an Executor.

R. Bemrose
A: 

Ya keep it simple. First of all the function names should be lowercased. Then, nstead of something like PeformReportGeneration, you want generateReport. It conveys the same message more succinctly.

In your case it's harder since Workflow isn't really a verb... how about instead of PerformPayPalWorkflow you just do run, e.g.:

PayPalWorkflow workflow = ...;
workflow.run()

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.

Claudiu
Convention is lowercase for Java and uppercase for C#.
bloparod