English 中文(简体)
指明一种方法的内容[封闭]
原标题:What to name a method [closed]
  • 时间:2010-03-31 21:07:34
  •  标签:
  • c#
Closed. This question is opinion-based. It is not currently accepting answers.

我辩论这一方法的名称。

密切现金交易(现金,Id,-1,真实);

全面现金交易(现金,Id,-1,真实);

或 neither are good?

In business terms/process by sending in these 3 values I m essentially "closing the transaction" 或 "completing the transaction" in our w或kflow.

However on the developer side, I cant infer wtf "Complete" 或 "Close" means. It f或ces me to look into the internals of the method. My struggle is that I try to name methods to infer what they are doing. Complete is just way too general and f或ces the consumer of the method to dive into the code every time I use w或ds 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 n或 the method name really infer what s going on.

问题回答

从C#编码标准......

6.8 Methods Methods should be named using the following format:

Verb + Adjective(s) + Noun + Qualifier(s)

例:

private Ball FindRedCansByPrice( 
     float price, 
  ref int canListToPopulate,
     out int numberOfCansFound )

准则:

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

  • 如同一类组别内的方法一起形成一个区域,并按使用频率排列(即更经常称为方法的组别应靠近其区域的顶端)。

希望会有助于


<><0>BONUS

Naming Parts & Pairs

  1. Common Adjective Pairs

    • Old…/New…
    • Source…/Destination…
    • Source…/Target…
    • First…/Next…/Current…/Previous…/Last…
    • Min…/Max…
  2. 共同财产

    • Allow… (Allows…)
    • Can…
    • Contains…
    • Has…
    • Is…
    • Use… (Uses…)
  3. 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…
  4. 共同质量 Suffixes

    • …Avg
    • …Limit
    • …Count
    • …Ref
    • …Entry
    • …Sum
    • …Index
    • …Total

<>说明: 避免使用Num,因为语气;使用指数和数字。 此外,避免使用温度;需要时间来描述该物体的真实情况(例如使用SapValue而不是TmValue)。

我的反馈

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

这些方法的类别是什么? 参数如何? 某些情况良好......

为什么没有<代码> 现金转账类别(或I Transaction接口和Cash Transaction 实施),该类别有<编码>Close方法或Complete?

如果不了解这一类别的名称,以及你预计会发出的各种电话,那么这种方法的名称就很难选定。

如果该类别的名称是“现金交易”,则“封闭”:

CashTransaction transaction = new CashTransaction();
...
transaction.close(...);

似乎很自然。

如果该类别的名称是“现金交易”,则该类别是建立、启动和完成的:

CashTransaction transaction = new CashTransaction();
...
transaction.initiate(...);
transaction.complete(...);

这也是非常自然的。

采用你自己的代码,并根据你允许的类别名称和电话模式来命名方法。 即便这并没有使最好的名字更清楚,但至少你们将能够在评论中记录理想的使用。

1. method形形形形形形形色,作为参数:

// declares the enum
public enum Action
{
   Close,
   Complete,
   Open,
   ...
}

使用:

ExecuteTransaction(Cash.Id, -1, true, Action.Close);

ExecuteTransaction(Cash.Id, -1, true, Action.Complete);

有些人告诉我: “货物(方法、类别等),这样,如果企业所有者看着该代码,就会给他们带来意义。”

由于英特尔利斯公司,没有任何借口不适当地描述某些物品,以便BA或商业类人能够理解其做些什么。

而且,总是有办法/等级特征来进一步解释给发展中国家,应当引证发生的事情。

我认为,你应该坐到<>。 近距离运输主要因为平均寿命在Aync电话中使用,而你的方法是t。

但是,最好的选择是设立现金转账类别,因为你的代码认为C而不是C#是这样写的。

也许我可以离开现场,但问题是什么(除了其他人评论的方法论点之外):

SettlePayment(Cash.Id, -1, true);

SettleCashPayment(Cash.Id, -1, true);

我倾向于避免以这种名义使用交易,因为这可能是一个超负荷的术语,可能造成混乱。 我也认为,接近和完全是相当模糊的。

从个人角度讲,我只读了较短的名字,即Im lazy。

然而,用什么来形容一种方法是你最担心的事。 仅凭<代码>////summary>的借项加以说明。

永远不会忘记你。 VS2008有一个重新设计的工具,可以重新命名方法,这样,如果你不喜欢这种方法,那么它就象10秒那样重新命名。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签