【问题标题】:C# naming conventions for a recursive inner functions?递归内部函数的 C# 命名约定?
【发布时间】:2011-10-19 05:41:03
【问题描述】:

C# 对常见的方法类型有一些命名约定:

  • BeginFoo() / EndFoo() 用于异步方法
  • TryGet() / TryParse() 返回 false 而不是抛出异常
  • FooOrDefault() 用于返回 default(T) 而不是抛出异常的方法
  • IsFoo 用于布尔标志

我想知道,有没有一种用于递归内部方法?例如在this example from another Stack Overflow question:

public int CalculateSomethingRecursively(int someNumber)
{
    return doSomethingRecursively(someNumber, 0);
}

// What to call this? 
private int doSomethingRecursively(int someNumber, int level)
{
    if (level >= MAX_LEVEL || !shouldKeepCalculating(someNumber))
        return someNumber;
    return doSomethingRecursively(someNumber, level + 1);
}

在 C 中,我看到人们使用 foo(...) + foo_r(...) 作为约定。但是在 .NET 中呢?

【问题讨论】:

  • +1 好问题 - 我记得我的一些大学课程使用“外壳”和“内核”。让我觉得自己像个农民。
  • 我有前缀私有方法,我打算用“Inner”递归调用,所以我可能有一个名为 Add() 的方法,然后是一个 InnerAdd()。
  • @DaveZiegler InnerAdd 是个好名字。我也用它。 MS 也有一个InnerAdd 方法(虽然不处理递归)。

标签: c# .net recursion naming-conventions


【解决方案1】:

我个人可能称它为CalculateSomethingRecursivelyImpl - 我倾向于使用Impl 作为“私有方法的后缀,它实际上完成了具有相同名称但没有后缀的方法的大部分工作。 "它是递归的这一事实对我来说不会改变这一点 - 但这只是个人选择。

但老实说,这样的方法可能总是私有的——所以它几乎没有公共/受保护的方法那么重要。只需与您的其他团队成员制定一个约定即可。

【讨论】:

  • @GeorgeDuckett:是的,没错。
【解决方案2】:

老实说,我会说适用于非递归函数的相同 C# 命名约定也适用于递归函数。尝试将其命名为描述性但相对较短的名称。如果你真的想不出与非递归不同的东西,那么你将不得不尝试使用后缀。我更喜欢“Helper”,虽然我尽量避免使用后缀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2012-10-08
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多