Private method naming convention [closed]

允我心安 提交于 2019-11-28 19:08:41

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

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.

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 implementation for the public method, one convention is to call it *Impl. I.e. AddImpl in your case.

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

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;
}
Lasse Vågsæther Karlsen

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

These are my naming conventions for 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

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.

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

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

The two variations that I've seen commonly used are these:

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

and

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

Neither one is particularly satisfactory, but they're what I've seen.

I've never seen a convention that ALL private methods should have a prefix - the mere thought of it makes me shudder!

It's bad enough dealing with all the C++ developers who prefix every member in sight with "_" - and I'm speaking as a former Delphi developer who used to prefix every member with "F". I'm still recovering from that!

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

Public methods:

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

Private methods:

private void _Add()
{
}
_Add();

Properties:

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

Fields:

private bool _isUsed;
_isUsed = false;

Local variables:

bool isUsed;

I'd go for whatever my teammates suggested and make it 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...

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:

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 :-)

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.

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.

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

Oh I forgot to mention just why I do this. It is because it is a simple way to create processing by using a loop for methods that I would like to call in sequence. For instance if I have a some validation that needs to be done slightly different each time but using methods with the same class. I set them up as

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
} 

EPiServer uses a convention of "...Internal" as in AddInternal() in this situation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!