私有方法命名约定

时间:2008-12-20 23:01:54

标签: c# coding-style private-methods

是否有一个约定我称之为“_Add”的私有方法的约定?我不是领先下划线的粉丝,但这是我的队友建议的。

public Vector Add(Vector vector) {
    // check vector for null, and compare Length to vector.Length
    return _Add(vector);
}

public static Vector Add(Vector vector1, Vector vector2) {
    // check parameters for null, and compare Lengths
    Vector returnVector = vector1.Clone()
    return returnVector._Add(vector2);
}

private Vector _Add(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

14 个答案:

答案 0 :(得分:28)

我经常看到并使用“AddCore”或“InnerAdd”

答案 1 :(得分:25)

我从未在C#中看到任何区分公共和私有方法的编码约定。我不建议这样做,因为我没有看到好处。

如果方法名称与公共方法冲突,则应该更具描述性;如果,在您的情况下,它包含公共方法的实际方法实现,则一种约定是将其称为*Impl。即在你的情况下AddImpl

答案 2 :(得分:24)

我通常将thisCase用于私有方法,将ThatCase用于公共方法。

private Vector add(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

public Vector Add(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

答案 3 :(得分:15)

就个人而言,无论可见性如何,我都有相同的命名约定。

这些是我对C#的命名约定:

  • 命名空间,类型,方法,属性:PascalCase
  • 局部变量:camelCase
  • 方法参数:camelCase
  • 私有字段:带有下划线前缀的_PascalCase,如果是属性的后备字段,那么同名的属性只有下划线前缀

编辑:注意,我对使用私有方法的前缀名称感到内疚。我第一次阅读时没有抓到你问题的那一部分。

例如,如果我有7种不同的方式通过我的DatabaseCommand类执行我的SQL语句,如QueryDataTable,QueryEnumerable,QueryEnumerable<T>,QueryDataReader等,那么所有这些都想调用相同的私有方法,我倾向于将此方法称为InternalQuery或PrivateQuery。

答案 4 :(得分:5)

由于公共Add()进行了一些检查而私有没有:

private Vector AddUnchecked(Vector vector) {
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

答案 5 :(得分:5)

我经常使用的两种变体是:

private Vector DoAdd(Vector vector) { ... }

private Vector AddImpl(Vector vector) { ... }

两者都不是特别令人满意,但它们就是我所见过的。

我从未见过所有私有方法都应该有前缀的约定 - 仅仅想到它会让我不寒而栗!

处理所有使用“_”为每个成员添加前缀的C ++开发人员已经足够糟糕了 - 我说的是前Delphi开发人员,他曾经用“F”为每个成员加前缀。我还在恢复!

答案 6 :(得分:3)

对于私有属性使用前导下划线是很常见的,但我从未在方法

上看到它

答案 7 :(得分:3)

公共方法:

public void Add()
{
}
this.Add()

私人方法:

private void _Add()
{
}
_Add();

属性:

public int Id {get;set;}
this.Id = 10;

字段:

private bool _isUsed;
_isUsed = false;

局部变量:

bool isUsed;

答案 8 :(得分:1)

我认为在大多数惯例中,私人物品都有更多的自由。但是我看到了很多:

private Vector AddCore(Vector vector)

private Vector DoAdd(Vector vector)

但是我可能会删除私有的add方法,只有一个:

public static Vector Add(Vector vector1, Vector vector2) 
{
    // check if vector1 is null
    Vector returnVector = vector1.Clone()
    return returnVector.Add(vector2);
}

public Vector Add(Vector vector) 
{
    // check parameters for null, and compare Lengths
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

还将那些花括号放在正确的位置: - )

答案 9 :(得分:1)

我会为队友建议的任何事情而努力,使成为团队中的一项惯例。但在特定情况下,您似乎可以避免它:

public Vector Add(Vector vector) {
    // check vector for null, and compare Length to vector.Length
    for (int index = 0; index < Length; index++) {
        this[index] += vector[index];
    }
    return this;
}

public static Vector Add(Vector vector1, Vector vector2) {
    // check parameters for null, and compare Lengths
    Vector returnVector = vector1.Clone()
    return returnVector.Add(vector2);
}

或者也许我不应该迟到这么做......

答案 10 :(得分:0)

使用方法名称描述以创建解释自身的代码,不应该有任何冲突,因为你不应该有两个方法做同样的事情,所以你没有理由需要一个字符来加前缀方法名字用。

有些人使用“m_”为私有字段添加前缀,我没有看到私有方法的任何类似样式。

答案 11 :(得分:0)

遗憾的是,我已经遇到过这样的约定,并且倾向于在带有前导下划线的表单上命名控件(例如“_txtFirstname”而不仅仅是“txtFirstname”)。这似乎是一种奇怪的溢出效应,来自不再使用MS推荐的使用前导下划线命名私有变量的做法。那个,或者程序员只是喜欢使用下划线键,我无法理解。

不要使用这个惯例,当你的同事坚持这个约定时,要求他在网上找到推荐这种做法的东西(任何)。

答案 12 :(得分:0)

哦,我忘了提到我为什么要这样做。这是因为它是一种通过使用循环来创建处理的简单方法,我想按顺序调用这些方法。例如,如果我有一些验证需要每次稍微不同,但使用相同类的方法。我把它们设置为

add_process = array(1,2,3);
delete_process = array(2,6,4);

//delete user, users  posts and users comments
foreach( process2 as value){
 method_{value}_delete
} 

答案 13 :(得分:0)

在这种情况下,EPiServer使用“{Internal”的约定,如AddInternal()