可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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;
}
回答1:
I usually see and use either "AddCore" or "InnerAdd"
回答2:
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.
回答3:
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;
}
回答4:
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.
回答5:
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!
回答6:
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;
}
回答7:
It is quite common to use a leading underscore for private properties but I have never seen it done on methods
回答8:
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;
回答9:
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...
回答10:
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 :-)
回答11:
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.
回答12:
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.
回答13:
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
}
回答14:
EPiServer uses a convention of "...Internal" as in AddInternal()
in this situation.