tags:

views:

1403

answers:

13

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.

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;
}
+10  A: 

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.

Konrad Rudolph
In addition, most IDE's have auto suggest that filter out private methods or hide them.
daub815
Also, if you change a method from public to private or vice versa, you really don't want to have to change the name of the method, and you shouldn't need to
Bazman
You're missing the point... He CANT name it "Add" since it would be a dupe. He's looking for an industry best practice for this situation.
TheSoftwareJedi
+1  A: 

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

Greg Dean
+1  A: 

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

cgreeno
+1  A: 

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.

Paul
+6  A: 

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

These are my naming conventions for C#:

  • Namespaces, types,methods, properties: CamelCase
  • Local variables: mixedCase
  • Parameters to methods: mixedCase
  • Private fields: _CamelCase 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.

Lasse V. Karlsen
+1  A: 

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

PEZ
+4  A: 

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;
}
Andy May
+7  A: 

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

TheSoftwareJedi
@Greg Dean Tell me about it... I don't think people understand what was being asked!
TheSoftwareJedi
I like both of your suggestions. Thank you.
Jason
A: 

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.

MusiGenesis
A: 

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
}
Carl McDade
A: 

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;
}
EricSchaefer
A: 

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!

Bevan
I also shudder. But please note that my question was for a convention in a situation where there is a private method doing the work behind some public methods. Hence "[i]s there a convention for naming the private method that I have called "_Add" here?" and the sample code presented.
Jason
A: 

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;
JohnKZ