views:

269

answers:

7

I'm a web developer with no formal computing background behind me, I've been writing code now some years now, but every time I need to create a new class / function / variable, I spend about two minutes just deciding on a name and then how to type it.

For instance, if I write a function to sum up a bunch of numbers. Should I call it

Sum()
GetSum()
getSum()
get_sum()
AddNumbersReturnTotal()

I know there is a right way to do this, and a link to a good definitive source is all I ask :D

Closed as a duplicate of c# Coding standard / Best practices

+5  A: 

You're looking for StyleCop.

chills42
+1  A: 

All of the above.

I believe the official C# guidelines would say call it calculateSum() as getSum() would be used if the sum was an instance variable. But it depends on the coding style used and how any existing code in the project is written.

Thomas Owens
A: 

Luckily enough I don't believe there is a standardized way this is done. I pick the one that I like, which consequently also seems to be the standard all other source code I've seen uses, and run with it.

Jeremy Reagan
Did you go to Harding?
Joel Coehoorn
I'm afraid I don't understand the question, Harding?
Jeremy Reagan
A: 

Sum() if it's public and does the work itself.

GetSum() if it's public and it retrieves the information from somewhere else.

sum() / getSum() as above, but for internal/private methods.

(Hmm... That's a bit vague, since you shift the meaning of "Sum" there slightly. So, let try this again.

XXX if xxx is a process (summing values). GetXXX if xxx is a thing. (the sum of the values)

James Curran
+4  A: 

Classes should be in camel notation with the first letter capitalized

public class MyClass

Functions and Methods in C# should act in a similar fashion except for private methods

public void MyMethod()
private void myPrivateMethod()

Variables I tend to do a little differently:

Member Variables

private int _count;

Local variables

int count;
Nicholas Mancuso
camelCase looksLikeThis. PascalCase IsWhat YouAreThinkingOf.
Joel Coehoorn
I disagree with underscores on member variables.
Gary Willoughby
Gary, you like using this everywhere? Give me a reason. REASON!
Rob Stevenson-Leggett
exactly my logic Rob. I hate having to type 'this.' all the time. With intellisense it brings up a list of anything related to class when all i want is member variables. just typing '_' will automatically bring up a list of only my member variables. NICE!
Nicholas Mancuso
+2  A: 

I agree on the calculate vs get distinction: get() should be used for values that are already calculated or otherwise trivial to retrieve.

Additionally, I would suggest in many cases adding a noun to the name, so that it's obvious exactly what sum you are calculating. Unless, of course, the noun you would add is the class name or type itself.

Joel Coehoorn
A: 

Method names are verbs. Class, field and property names are nouns. In this case, Sum could pass as either a verb or a noun...

AddNumbersReturnTotal fits the above definition, but it's a little long. Out of kindness to the guy who gets to maintain my code (usually me!) I try and avoid including redundant words in identifiers, and I try to avoid words that are easy to make typos on.

Tim Robinson
Pairing Sum calculate or get turns method name into a verb, even if sum is itself a noun. Placing a Sum before a noun pretty much ensures it (and you method) will be treated as a verb.
Joel Coehoorn