Joey
Joey

Reputation: 91

Why are getters prefixed with the word "get"?

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.

Upvotes: 9

Views: 2968

Answers (8)

Friedrich
Friedrich

Reputation: 6006

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.

Upvotes: 1

James Sumners
James Sumners

Reputation: 14777

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.

Upvotes: 1

Frank Bollack
Frank Bollack

Reputation: 25176

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.

Upvotes: 4

Justin Niessner
Justin Niessner

Reputation: 245429

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.

Upvotes: 13

RedPandaCurios
RedPandaCurios

Reputation: 2324

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()

Upvotes: 3

Paul Nathan
Paul Nathan

Reputation: 40319

Custom.

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

Upvotes: 0

danben
danben

Reputation: 83250

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.

Upvotes: 6

Reynolds
Reynolds

Reputation: 414

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.

Upvotes: 2

Related Questions