Functions should begin with "Get"?

I am trying to take a look at C # coding standards to make my code more beautiful and standard, and I have a question: should functions (methods that are not voids), in accordance with C # coding standards, start with “Get”? For example: " GetSongOrder() ", " GetNextLine() ", etc.

Thanks.

+7
standards c # coding-style
source share
6 answers

There are two cases:

If the procedure can “get” the value very quickly without causing side effects in the code, I recommend using the getter property and omitting the name “get”:

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

If, however, processing is required, then a method called "Get ..." is appropriate. This suggests that there may be side effects to calling this method, such as additional 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 using Disk IO/etc // do other work? return line; } 
+24
source share

Your function name should be short and make sense. No more, no less.

If you work for you, use Get. If Retrieve works, use this.

There are no true "standards" for naming conventions.

+2
source share

In general, the Verb-Noun naming convention for functions is general. “Get” is just one of the verbs you can see. It can also be “Activate”, “Close”, “Dump” or something else ... it's just a consequence of the English and general coding that the method or function will usually “Do something” on “Some types of objects” ,.

+2
source share

I did not see the C # standard on this, but most of the actual code that I saw uses the getter property to use, 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 ​​what I usually see in langauges that don't have a la.NET properties (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 to use getter and setter, and you do not need additional processing, just pure Java-bean-like get / set youc do this:

 Public SongOrderList SongOrder { get; set; } 

If you only need the HEAD getter, you can do it (I think it was time):

 Public SongOrderList SongOrder { public get; private set; } 
+1
source share

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

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

There are other forms where you rather describe the result of a function, for example, the Substring function.

+1
source share

In addition to other answers - if you have a method like GetNextLine (), you might want to return an enum, for example

IEnumerable<Line> GetLines();

+1
source share

All Articles