views:

193

answers:

6

Hi, I'm trying to look at the C# Coding Standards to make my code more beautiful and standard, and I have a question: Does functions (methods that are not voids), according to the C# coding standards, should start with "Get"? For example: "GetSongOrder()", "GetNextLine()" etc?

Thanks.

+1  A: 

I haven't seen a C# standard on that, but most of the actual code I've seen written uses "getter" properties and they omit the word "Get" from the function name. Example:

Public SongOrderList SongOrder
{
    get
    {
      return mySongOrderList;
    }
}

Using "Get" (and "Set") as a function name prefix is something I usually see in langauges that don't have properties a la .NET (languages such as C, Java).

Edit:

...and of course you can have setters too

Public SongOrderList SongOrder
{
    get
    {
      //do some processing code here
      return mySongOrderList;
    }
    set
    {
      //do some processing code here
      mySongOrderList = value; //value is a C# keyword, in case you didn't know. it is the parameter to the setter
    }
}

Of course, if you want a getter and a setter and you don't need any extra processing, just pure Java-bean-like get/set youc an do this:

Public SongOrderList SongOrder
    {
        get; set;
    }

If you only want a PUBLIC getter you can do this (I think - it's been a while):

Public SongOrderList SongOrder
    {
        public get; private set;
    }
FrustratedWithFormsDesigner
Beat me to it. :) I think the tendency to want to use 'Get' comes from Java's frequent use of GetFoo()/SetFoo(value) pairs. In C# we typically wrap these up in a property, Foo.
JMD
and if there's a parameter?
TTT
Not always true, though - If there are side effects to calling this, it's better as a method named "Get", IMO.
Reed Copsey
@Reed Copsey: If there are side effects, I try to use a function and not use the word "Get" but I use "Retreive", "Extract", etc... Mostly because to *me* "get" implies *just* a "get" with no other side effects.
FrustratedWithFormsDesigner
+2  A: 

Your function name should be succinct, and make sense. Nothing more, nothing less.

If get works for you, then use Get. If Retrieve works, use that.

There is no true "standard" for naming conventions.

Jack Marchetti
I would say "The function name should be as succinct as possible while remaining self descriptive" - Don't go too short ;)
Reed Copsey
+1  A: 

In general, a Verb-Noun naming convention for Functions is common. 'Get' is just one of the Verbs you might see. It could also be 'Activate', 'Close', 'Dump', or anything... it's just a consequence of English and general coding that a method or function will typically 'Do Something' to 'Some Type of Object(s)'.

Michael Bray
+20  A: 

There are two cases:

If the routine can "get" the value very quickly, without causing side effects in the code, I recommend using a property getter, and omitting the "get" name:

public SongOrder SongOrder
{
   get { return this.songOrder; } // ie: an enum
}

If, however, there is processing required, then a method with the "Get..." name is appropriate. This suggests that there may be side effects to calling this method, such as extra processing, or a change in some state:

public string GetNextLine()
{
    string line = this.stream.ReadLine(); // This may cause a longer running operation, especially if it's using Disk IO/etc
    // do other work?
    return line;
}
Reed Copsey
Very good description. My thoughts exactly - Properties should never use Verb-Noun convention, and should just be Noun. Methods should always be Verb-Noun.
KP
Thank you for your answer, but what I mean is should you choose "GetNextLine" or "ReadLine" for example.
TTT
@Alon: GetNextLine and ReadLine could be the same thing or they could be different - it's not immediately clear. If you are reading from a file, use "ReadLine". If you are getting a line from an in-memory string, use "GetNextLine".
FrustratedWithFormsDesigner
+1  A: 

No, there is no standard that function should start with "Get".

When creating function names that make sense, they often take that form, like the GetHashCode function. Other verbs in the beginning are common, like in the ToString function, and only verbs like in the Encode function.

There are other forms, where you rather describe the result of the function, like the Substring function.

Guffa
+1  A: 

In addition to other answers - if you have a method like GetNextLine(), you may want to consider returning an enumeration instead e.g.

IEnumerable<Line> GetLines();

Lee