tags:

views:

141

answers:

7

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  A: 

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.

Justin Niessner
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
@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.
Robert Harvey
@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
+1  A: 

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.

Reynolds
Your school didn't get it right, IMO.
Robert Harvey
On the other hand, I approve of your personal opinion.
David Thornley
See here for appropriate use of getters and setters: http://stackoverflow.com/questions/565095/java-are-getters-and-setters-evil
Robert Harvey
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
+1  A: 

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.

danben
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
@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.
Robert Harvey
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
@danben: Unfortunately the example given is ambiguous; `name` can be either a noun *or* a verb.
Robert Harvey
@Robert Harvey - good point.
danben
+1  A: 

Custom.

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

Paul Nathan
Really? Do you think that operator[] of std::string (for example) causes such a leak?
anon
Neil: I don't per se mean memory leakage.
Paul Nathan
@Paul I didn't mean or mention memory leaks - I meant your own concept of "leakage".
anon
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.
Paul Nathan
@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
+1  A: 

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()
CuriousPanda
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
A: 

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.

Frank Bollack
This is a very good point, perhaps the strongest in my view.
Joey
A: 

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.

jsumners
@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". http://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
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.
jsumners
Awesome. Comments don't support markup.
jsumners
In C# 3.0, you can just do `public int Foo { get; set; }`
Robert Harvey