Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions should start with "Get"?

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.

like image 992
Alon Gubkin Avatar asked Jan 22 '10 21:01

Alon Gubkin


2 Answers

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;
}
like image 170
Reed Copsey Avatar answered Sep 18 '22 23:09

Reed Copsey


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.

like image 44
Jack Marchetti Avatar answered Sep 17 '22 23:09

Jack Marchetti