Why do getters have a prefix labeled "get"?

Generally speaking, creating an API fluid is what makes all programmers happy; And for the creators who write the interface, and for those who program against it. Looking beyond the conventions, why are we prefixing all of our getters with the word "get." Omitting this, as a rule, leads to a more flexible, easy-to-read set of instructions, which ultimately leads to happiness (albeit small or passive). Consider this very simple example. (pseudo code)

Usual:

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 are not directed to any particular language. Whether these conventions were developed around technical limitations (ambiguity), or simply by achieving a more explicit, intentional feeling like an interface, or perhaps this is just a case of a downward trickle. What do you think? And as if simple changes in these agreements affected your happiness / daily attitude to your craft (albeit minimal).

Thanks.

+6
conventions
source share
8 answers

Because in languages ​​without properties, name() is a function. However, without any additional information, it is not necessarily specific about what he is doing (or what he is going to return).

Functions / methods should also be verbs because they perform some action. name() clearly not in line with the bill because it does not report anything about what action it performs.

getName() lets you know without a doubt that the method will return a name.

In languages ​​with Properties, the fact that something is a property expresses the same meaning as when it was received or installed. It just makes things look a little neat.

+10
source share

The best answer I've ever heard to use get / set prefixes is this:

If you have not used them, then both the accessory and the mutator (getter and setter) will have the same name; this way they will be overloaded. As a rule, you should only overload the method when each implementation of the method performs a similar function (but with different inputs).

In this case, you will have two methods with the same name that form very different functions, and this can confuse API users.

+4
source share

I am always familiar with the sequential get / set prefix when working with the new API and its documentation. Automatic grouping of getters and setters, when all functions are listed in their alphabetical order, helps to distinguish between simple data access and advanced functionality.

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

+4
source share

What about when a property is named after a verb?

 object.action() 

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

 object.getAction() object.setAction(action) object.doAction() 
+3
source share

At school, we were taught to use to distinguish methods from data structures. I never understood why parsers would not be advice. I am of the opinion that excessive use of get / set methods can be a terrible time, and at this point I see that many object-oriented programmers go through them after they start.

+1
source share

Here is a small answer that I like best. You need to know a few rules about Smalltalk BTW. fields are only available if they are defined. If you do not record "accessors", you cannot do anything with them.

There is a variable in the convention (let anme it instVar1. Then you write the function instVar1, which simply returns instVar1 and instVar: which sets the value.

I like this convention much more than anything else. If you see: somewhere you can argue with this “setter” in one way or another.

+1
source share

Custom.

Also, in C ++, if you return a link that provides a potential leak of information in the class itself.

0
source share

I can't write much Objective-C, but since I found out, I really liked its conventions. What you ask for is addressed in language.

0
source share

All Articles