3

I came across a recent Audit report in our company for the code we maintain which says that we should not use Get in the method (not properties) naming like in GetSearchResults or GetXyzInformation. I looked up the MS guidelines (http://msdn.microsoft.com/en-us/library/4df752aw(VS.71).aspx) for method naming and as per that Get is allowed, so what are your thoughts on this, Can we have it or not from a standard perspective, if not why?

Additional Info
I am adding more information after the first set of answers, the Methods that i am referring to are the kind which involves a database interaction, so clearly properties are not ideal options.

3
  • I don't see where you get the idea that it is not allowed. Your coding standard only says that you should not use the word. Not that it is in any way illegal according to the language or the standard.
    – jalf
    Feb 24, 2009 at 13:40
  • Apart from that, is there any particular reason why you didn't write the 3 lines of code needed to just test if it was legal? ;)
    – jalf
    Feb 24, 2009 at 13:41
  • Jalf, when the comment comes in an audit report as you should not use (not avoid using), it means that we failed a recomended standard, This is existing code, so it was alredy written. Feb 24, 2009 at 13:45

8 Answers 8

14

Yes, they certainly can - there are plenty of examples in the standard API (e.g. Delegate.GetInvocationList).

However, generally it's worth considering whether you actually want a property when you create a GetFoo method.

1
  • Had to go with Jon on this one just because, well, just because it's Jon. ;) Feb 24, 2009 at 21:59
9

It is perfectly legal (it is even legal to write methods of the form "get_Foo" which would collide with the compiler synthesized get method if you were to declare a property Foo with a getter).

As to Whether it is a good thing:

  1. If the method does serious work, has side effect or may throw an exception is it probable that it is not a good candidate for a property and the name is descriptive so should not be changed needlessly
  2. If the method does no serious work making it a property is probably a good thing since it will make it easier to work with in calling code.
  3. if the method does not actually 'Get' anything the name is bad and should be changed

The above points are subjective but the reason that hard rules like 'never' 'always' can be a bad idea is that the balancing of multiple subjective and competing factors is part of what being a good developer is all about.

9

You can do it, yes, but it is often a strong indication that you should really be creating a property (especially if your method has no parameters).

From the Framework Design Guidelines:

DO NOT have properties that match the name of "Get methods" as in the following example:

public string TextWriter { get {...} set {...}}
public string GetTextWriter(int value) {...}
3

If your Get-method accepts parameters, it's absolutely OK to name it accordingly.

If it doesn't accepts parameters, rename it to SearchResults { get; }

And it's much-much better then use parametric properties like in VB.NET:

ReadOnly Property Foo(ByVal i As Integer, ByVal j As Integer) As String
  Get
    Return ""
  End Get
End Property

In fact, in IL it will be method GetFoo(int, int) but VB.NET is wonderful language!

2
  • In addition to parameters, I would also consider how much work is going to be done. To most people, property access implies a relatively simple operation behind the scenes (often just accessing a backing variable); anything complicated or slow probably belongs in a method. Feb 24, 2009 at 15:19
  • Agree about design simplicity - Property is quick getter, method - is some job. But in fact, in IL, Property equals to Method, because this is only CLR semblance: property accessors will be appropriate methods Feb 24, 2009 at 15:23
2

Yes, I'd indeed use GetXyz if the method has some side effects, is slow to execute or requires arguments (all cases where you definitely should not use an Xyz property).

Personally, I mix and match GetXyz and FindXyz in cases where returning null might not be the right thing to do for a get. A get could allocate a new object if it does not find what it is looking for, whereas a find would return null, for instance.

1

Absolutely you can use Get in the method name. Personally I use it to make this distinction:

Not called Get:

public string GetSomeString()
{
     return _someMemberVariable;
}

Edit: It seems I wasn't clear here, I meant to show that properties with Get in the name are misleading if they do no processing.

The word Get in a method name implies effort required.

public string GetSomeString()
{
     //Some code that does processing or includes logic.
}

I believe this is mentioned in Clean Code as a good practice (to make clear that it's not simply handing back a value).

2
  • Ouch, I think this is certainly not a good practice, since it adds confusion. Feb 24, 2009 at 13:45
  • I think my code formatting confused this somewhat. Updated to make it more clear. Feb 24, 2009 at 14:33
1

I use a technique with similarities to some of the above examples - my criteria is that if the property/method is exposing data that already is held in the class, use a property. If we are triggering some kind of action to go obtain the requested value, say from a database, or some other source not already stored inside the class, use a Get.

Oversimplified, properties are nouns and methods are verbs.

0

It is quite natural to use Get as the begin of method name in my opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.