9

Generally speaking, creating a fluid API is something that makes all programmers happy; Both for the creators who write the interface, and the consumers who program against it. Looking beyond conventions, why is it that we prefix all our getters with the word "get". Omitting it usually results in a more fluid, easy to read set of instructions, which ultimately leads to happiness (however small or passive). Consider this very simple example. (pseudo code)

Conventional:

person = new Person("Joey")
person.getName().toLower().print()

Alternative:

person = new Person("Joey")
person.name().toLower().print()

Of course this only applies to languages where getters/setters are the norm, but is not directed at any specific language. Were these conventions developed around technical limitations (disambiguation), or simply through the pursuit of a more explicit, intentional feeling type of interface, or perhaps this is just a case of trickle a down norm. What are your thoughts? And how would simple changes to these conventions impact your happiness / daily attitudes towards your craft (however minimal).

Thanks.

6
  • 4
    Some of us don't write getters. Or setters. Or believe in "fluid APIs".
    – anon
    Apr 15, 2010 at 19:53
  • 1
    I disagree that person.name() is a more fluid and easier set to read set of instructions, as I would think that the method actually assigns a name, or a nickname, to this person. And honestly, if you intend to use it in that context, person.name.whatever looks and reads much easier as now, name becomes a (readable/assignable) property instead of a method.
    – rlb.usa
    Apr 15, 2010 at 19:57
  • 2
    so when you have and IDE with code completion, you can type "get" and see all the getters
    – colinjwebb
    Apr 15, 2010 at 20:14
  • @rlb We are under the assumption that the said (pseudo) language does not have property accessors. In such a situation, if one want's to alter the implementation, name(), a method, is the best one can do.
    – Joey
    Apr 15, 2010 at 20:16
  • 2
    And also because it's a semi-convention already. It's a not re-inventing the wheel, predictability-equals-understandability, kind of thing.
    – rlb.usa
    Apr 15, 2010 at 20:41

8 Answers 8

13

Because, in languages without Properties, name() is a function. Without some more information though, it's not necessarily specific about what it's doing (or what it's going to return).

Functions/Methods are also supposed to be Verbs because they are performing some action. name() obviously doesn't fit the bill because it tells you nothing about what action it is performing.

getName() lets you know without a doubt that the method is going to return a name.

In languages with Properties, the fact that something is a Property expresses the same meaning as having get or set attached to it. It merely makes things look a little neater.

3
  • Agreed. This falls along the case i mentioned as being explicit about things intentionally as to not create any kind of ambiguity. However, I am under the belief that a well designed API would use the right names to obscure any ambiguity in that manner. As a challenge, can you give me an example in which the lack of the prefix would cause you to think you don't know whats going on?
    – Joey
    Apr 15, 2010 at 19:59
  • @Joey: The problem with your naming arrangement is not the names but the parens. The parens suggest executing an action, rather than returning the value of a property. That's why the get prefix is needed. Apr 15, 2010 at 20:01
  • @robert very valid point. In situations where such a language does not have any mechanism to implement property accessors, or after the fact changes to a property implementation, using parers around the naked name is really the only situation.
    – Joey
    Apr 15, 2010 at 20:19
6

The best answer I have ever heard for using the get/set prefixes is as such:

If you didn't use them, both the accessor and mutator (getter and setter) would have the same name; thus, they would be overloaded. Generally, you should only overload a method when each implementation of the method performs a similar function (but with different inputs).

In this case, you would have two methods with the same name that peformed very different functions, and that could be confusing to users of the API.

4
  • 1
    Nice observation. Setters, i believe, very rightfully deserve the prefix, as they change some sort of state. It's not a zero sum game, ridding of the "get" does not necessarily lead to ridding of the "set". What if only the set were used, and thus avoiding overloading?
    – Joey
    Apr 15, 2010 at 20:02
  • @Joey: That sounds like more noise than just using get and set, as one doesn't look like the other. And you still have the problem with parens suggesting an action to be executed, rather than a property to be retrieved. Apr 15, 2010 at 20:05
  • I would be ok with it if the convention were that every method that did not have a verb in its name was an accessor.
    – danben
    Apr 15, 2010 at 20:05
  • Unfortunately the example given is ambiguous; name can be either a noun or a verb. Apr 15, 2010 at 20:07
4

I always appreciate consistent get/set prefixing when working with a new API and its documentation. The automatic grouping of getters and setters when all functions are listed in their alphabetical order greatly helps to distinguish between simple data access and advanced functinality.

The same is true when using intellisense/auto completion within the IDE.

0
3

What about the case where a property is named after an verb?

object.action()

Does this get the type of action to be performed, or execute the action... Adding get/set/do removes the ambiguity which is always a good thing...

object.getAction()
object.setAction(action)
object.doAction()
2
  • Thanks for the response curious. Can you give me an example in which a property named after a verb could also be logically used as a getter?
    – Joey
    Apr 15, 2010 at 20:07
  • "action" is not a verb so this is a bad example. In general, properties are always named after nouns, not verbs. Jun 8, 2015 at 17:39
2

In school we were taught to use get to distinguish methods from data structures. I never understood why the parens wouldn't be a tipoff. I'm of the personal opinion that overuse of get/set methods can be a horrendous time waster, and it's a phase I see a lot of object oriented programmers go through soon after they start.

3
  • 1
    On the other hand, I approve of your personal opinion. Apr 15, 2010 at 20:17
  • See here for appropriate use of getters and setters: stackoverflow.com/questions/565095/… Apr 15, 2010 at 20:33
  • Thanks for linking that, I think that brings up some great points. I didn't want to say that they were evil altogether, just that, as pointed out on the other question, there's not much reason for having get/setters if get methods always just return the same variable, and if setters always just allow arbitrary assignment. Java is supposed to eliminate boilerplate code, not promote it. I think we agree on that. Thanks for the link Robert!
    – Reynolds
    Apr 15, 2010 at 20:58
1

I may not write much Objective-C, but since I learned it I've really come to love it's conventions. The very thing you are asking about is addressed by the language.

3
  • 1
    @jsumners many circles around apple really do not use this convention. (via webkit style gudie). "Precede setters with the word "set". Use bare words for getters. Setter and getter names should match the names of the variables being set/gotten". webkit.org/coding/coding-style.html. Of course certain languages have abandoned such conventions because they have conventions or language constructs to work around them. Mileage may vary.
    – Joey
    Apr 15, 2010 at 20:35
  • Well, I have to use C# at work. C# has properties, but they are annoying (as the original question alluded). For example, this won't work: <pre><code> public class MyClass { private int foo; public int foo { get { return this.foo; } set { this.foo = value; } } } </code></pre> Instead, you have to do: <pre><code> public class MyClass { private int _foo; public int foo { get { return this._foo; } set { this._foo = value; } } } </code></pre> I just think Objective-C is a little less annoying in this regard. Apr 15, 2010 at 20:44
  • In C# 3.0, you can just do public int Foo { get; set; } Apr 15, 2010 at 20:56
1

Here's a Smalltalk answer which I like most. One has to know a few rules about Smalltalk BTW. fields are only accessible in the they are defined.If you dont write "accessors" you won't be able to do anything with them.

The convention there is having a Variable (let's anme it instVar1. then you write a function instVar1 which just returns instVar1 and instVar: which sets the value.

I like this convention much more than anything else. If you see a : somewhere you can bet it's some "setter" in one or the other way.

0

Custom.

Plus, in C++, if you return a reference, that provides potential information leakage into the class itself.

5
  • Really? Do you think that operator[] of std::string (for example) causes such a leak?
    – anon
    Apr 15, 2010 at 20:14
  • Neil: I don't per se mean memory leakage. Apr 15, 2010 at 20:33
  • I didn't mean or mention memory leaks - I meant your own concept of "leakage".
    – anon
    Apr 15, 2010 at 20:49
  • Not in that case. OTOH, if you were trying to create an immutable string, returning a reference to an element of the char array would pose a problem. Apr 15, 2010 at 20:58
  • @Neil: Yes, string's op[] does violate encapsulation (but that doesn't have to be a bad thing). For example, you can't change std::wstring's storage to use a MBCS and still meet the op[] interface requirement.
    – Roger Pate
    Apr 17, 2010 at 11:06

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.