C# 私有方法命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/383850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:24:13  来源:igfitidea点击:

Private method naming convention

c#coding-styleprivate-methods

提问by jason

Is there a convention for naming the private method that I have called "_Add" here? I am not a fan of the leading underscore but it is what one of my teammates suggests.

是否有命名我_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;
}

采纳答案by TheSoftwareJedi

I usually see and use either "AddCore" or "InnerAdd"

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

回答by Konrad Rudolph

I've never seen any coding convention in C# that distinguished between public and private methods. I don't suggest doing it, since I don't see the benefit.

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

If the method name conflicts with public methods, it's time to become more descriptive; if, as in your case, it contains the actual method implementationfor the public method, one convention is to call it *Impl. I.e. AddImplin your case.

如果方法名称与公共方法冲突,则是时候变得更具描述性了;如果在您的情况下,它包含公共方法的实际方法实现,则一种约定是调用它*Impl。即AddImpl在你的情况下。

回答by Greg Dean

I think with most conventions there is more freedom on private stuff. However I see this a lot:

我认为对于大多数约定,私人事物有更多的自由。但是我经常看到这个:

private Vector AddCore(Vector vector)

or

或者

private Vector DoAdd(Vector vector)

However I would probably drop the private add method and just have one:

但是我可能会放弃私有 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;
}

Also put those curly brackets in the right spot :-)

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

回答by cgreeno

It is quite common to use a leading underscore for private properties but I have never seen it done on methods

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

回答by Paul

Be descriptive with the method names to create code that explains itself, there shouldn't be any collisions because you shouldn't have two methods doing exactly the same thing, so you should have no reason to need a character to prefix method names with.

使用方法名称进行描述以创建解释自身的代码,不应该有任何冲突,因为您不应该让两个方法做完全相同的事情,因此您没有理由需要一个字符来作为方法名称的前缀。

Some people prefix private fields with "m_", I have not seen any similar styles for private methods.

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

回答by Lasse V. Karlsen

Personally, for methods, I have the same naming convention regardless of visibility.

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

These are my naming conventions for C#:

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

  • Namespaces, types,methods, properties: PascalCase
  • Local variables: camelCase
  • Parameters to methods: camelCase
  • Private fields: _PascalCase with underscore prefix, if backing field for property, then same name as property only with underscore prefix
  • 命名空间、类型、方法、属性:PascalCase
  • 局部变量:camelCase
  • 方法参数:camelCase
  • 私有字段:_PascalCase 带下划线前缀,如果支持属性字段,则与属性相同的名称仅带下划线前缀

Edit: Note, I am guilty of using prefix-names for private methods. I didn't catch that particular part of your question the first time I read it.

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

For instance, if I have 7 different ways to execute my SQL statement through my DatabaseCommand class, like QueryDataTable, QueryEnumerable, QueryEnumerable<T>, QueryDataReader, etc. then all of these wants to call the same private methods, I have a tendency to call this method InternalQuery or PrivateQuery.

例如,如果我有 7 种不同的方式通过我的 DatabaseCommand 类来执行我的 SQL 语句,比如 QueryDataTable、QueryEnumerable QueryEnumerable<T>、QueryDataReader 等,那么所有这些都想调用相同的私有方法,我倾向于调用这个方法 InternalQuery或私人查询。

回答by PEZ

I'd go for whatever my teammates suggested and makeit a convention in the team. But in the particular case it looks like you could avoid it:

我会按照队友的建议去做,并将其作为团队的惯例。但在特定情况下,您似乎可以避免它:

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);
}

Or maybe I just shouldn't be on SO this late...

或者也许我不应该这么晚来……

回答by Andy May

I usually use thisCase for private methods and ThatCase for public methods.

我通常将 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;
}

回答by MusiGenesis

I've encountered this convention a lot, unfortunately, along with a tendency to name controls on a form with a leading underscore (e.g. "_txtFirstname" instead of just "txtFirstname"). It seems to be a weird bleedover effect from the no-longer-MS-recommended practice of naming private variables with a leading underscore. That, or programmers just love using the underscore key for no reason I can figure out.

不幸的是,我经常遇到这种约定,并且倾向于使用前导下划线命名表单上的控件(例如“_txtFirstname”而不是“txtFirstname”)。不再推荐 MS 推荐的使用前导下划线命名私有变量的做法似乎是一种奇怪的溢出效应。那,或者程序员只是喜欢使用下划线键,我想不出任何原因。

Don't use this convention, and when your co-worker insists on it, challenge him to find something (anything) online that recommends this practice.

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

回答by EricSchaefer

Since the public Add() does some checks and the private doesn't:

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

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