What to name a method [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this questionI'm debating what to name this method.
CloseCashTransaction(Cash.Id, -1, true);
or
CompleteCashTransaction(Cash.Id, -1, true);
or neither are good?
In business terms/process by sending in these 3 values I'm essentially "closing the transaction" or "completing the transaction" in our workflow.
However on the developer side, I cant' infer wtf "Complete" or "Close" means. It forces me to look into the internals of the method. My str开发者_如何学Cuggle is that I try to name methods to infer what they are doing. Complete is just way too general and forces the consumer of the method to dive into the code every time I use words like this.
When I see stuff like this all over code, I have to take so much time to figure out what they are actually doing. And if the comments suck, I end up having to look at all logic in that method because the comments nor the method name really infer what's going on.
From C# coding standards....
6.8 Methods Methods should be named using the following format:
Verb + Adjective(s) + Noun + Qualifier(s)
Example:
private Ball FindRedCansByPrice(
float price,
ref int canListToPopulate,
out int numberOfCansFound )
Guidelines:
Parameters should be grouped by their mutability (from least to most mutable) as shown in the example above.
If at all possible, avoid exiting methods from their middles. A well written method should only exit from one point: at its end.
Avoid large methods. As a method’s body approaches 20 to 30 lines of code, look for blocks that could be split into their own methods and possibly shared by other methods.
If you find yourself using the same block of code more than once, it’s a good candidate for a separate method.
Group like methods within a class together into a region and order them by frequency of use (i.e. more frequently called methods should be near the top of their regions.
Hope it helps
BONUS
Naming Parts & Pairs
Common Adjective Pairs
- Old…/New…
- Source…/Destination…
- Source…/Target…
- First…/Next…/Current…/Previous…/Last…
- Min…/Max…
Common Property Prefixes
- Allow… (Allows…)
- Can…
- Contains…
- Has…
- Is…
- Use… (Uses…)
Common Verb Pairs
- Add…/Remove…
- Insert…/Delete…
- Increment/…Decrement…
- Lock…/Unlock…
- Begin…/End…
- Fetch…/Store…
- To…/From… (Convert implied)
- Open…/Close…
- Create…/Destroy…
- Acquire…/Release…
- Up…/Down…
- Show…/Hide…
- Start…/Stop…
Common Qualifiers Suffixes
- …Avg
- …Limit
- …Count
- …Ref
- …Entry
- …Sum
- …Index
- …Total
Note: Avoid using Num because of semantics; use Index and Count instead. Also, avoid using Temp; take the time to describe what the object really is (e.g. use SwapValue instead of TempValue).
Here's my feedback
- Boolean parameters are generally speaking evil. As a casual observer I look at your method see
true
and have absolutely no idea what that means. Instead of a boolean you should either have 2 methods or use an enum instead. I wrote a more detailed discussion of this issue on my blog (link) - The use of -1 here is similar to the use of the boolean in that it adds no value to the reader. Instead use a named constant or a method with a different name and remove the parameter altogether.
What is the class that these methods are on? What do the parameters do? Some context would be good...
Why not have a CashTransaction
class (or a ITransaction
interface and a CashTransaction
implementation) which has a Close
method or a Complete
method?
The name of the method cannot easily be chosen without knowing the name of the class, and the pattern of calls you expect to make.
If the Class is named "CashTransaction" and created and then "closed" then:
CashTransaction transaction = new CashTransaction();
...
transaction.close(...);
seems very natural.
If the class is named "CashTransaction" and it is created, initiated, and completed, then:
CashTransaction transaction = new CashTransaction();
...
transaction.initiate(...);
transaction.complete(...);
would also be very natural.
Try using your own code, and name the methods based on the context of the class name and call pattern you allow. Even if that doesn't make the best name to pick much clearer, at least you will be able to document an example of idealized usage in the comments.
What about passing an enum type, as a parameter, to the method :-
// declares the enum
public enum Action
{
Close,
Complete,
Open,
...
}
Usage :-
ExecuteTransaction(Cash.Id, -1, true, Action.Close);
or
ExecuteTransaction(Cash.Id, -1, true, Action.Complete);
Something someone told me may help: "Name things (methods, classes, etc) so that if the business owner had a look at the code, it would make sense to them."
With Intellisense there's no excuse not to properly name things descriptively enough so that a BA or business-type person could understand what it does.
And there's always method/class attributes to further explain to devs should exciting things be happening therein.
I think you should go with CloseCashTransaction mainkly because verb Complete is used in Async calls and your method isn't one.
But the best option is to create CashTransaction class as your code looks like it is written in C, not C#. For better guide look at Edwin Buck's post.
Maybe I'm way out in left field, but what about (leaving aside the method arguments which others have commented on):
SettlePayment(Cash.Id, -1, true);
or
SettleCashPayment(Cash.Id, -1, true);
I would tend to avoid the use of Transaction in the name since that could be an overloaded term that could cause confusion. And I agree that Close and Complete are pretty vague.
Personally, I'd go with the shorter name cause I'm lazy.
Seriously though, what to name a method is the least of your worries. Just be sure you have a ///summary
bit describing it.
Don't forget you can always refactor. VS2008 has a built in refactoring tool to rename methods, so if you don't like it later it literally takes like 10 seconds to rename it.
精彩评论