What is the best practice of using get in method names?

I noticed in many places in Java (including C #) that many getter methods are prefixed with get, and many others are not. I never noticed that some kind of Sun pattern seemed to follow it. What are some guidelines or rules for using get in getter method names?

+4
source share
9 answers

It comes down to semantics. Yes, C # has “properties” that give you the get / set 'method' stub method ... but functions (... "methods" ...) in the .NET Framework that start with "Get" should be developed by the developer in that some operations occur for the sole purpose of obtaining certain results.

You might think this is strange and say, “Why not just use the return type to identify people in?” And the answer is simple. Think of the following methods:

public Person CreatePerson(string firstName, string lastName) {...} 

Just by the name of this method, you can probably see that the database activity will be activated, and then the newly created “person” will be returned.

but how about this:

 public Person GetPerson(string firstName, string lastName) {...} 

Just by this method name you can assume that 100% “safe” extraction of a person from the database is performed.

You will never call CreatePerson a few times ... but you must feel safe to constantly call GetPerson. (it should not affect the "state" of the application).

+4
source

"get" and "set" a prefix pair in Java is used initially as a convention for java bean. This later became simply an encapsulation agreement, since Java, unlike C #, does not have the proper properties.

+4
source

The best practice in Java is to use get and set prefixes for properties.

Frames, tag libraries, etc. will search for methods with these prefixes and use them as properties.

So, if you have such a java class ...

 public class User{ private String name; public String getName(){ return name;} public void setName(String name){ this.name = name; } } 

.. using stuts-tags (or any other ognl-based tag library) you will access the name property using user.name .

The Spring framework also uses this convention in xml configuration files.

+4
source

Java does not (yet) support property support. Getters and setters are a means to get around this. Other languages, including C #, support properties, and you should use them instead. This is not just a “best practice”: serialization in C # will be based on properties, not on getters and setters, so not using properties can cause all problems in the future if you need to serialize your classes.

The advantage of properties is that they make the code more readable. Sort of

 obj.setX(10); 

in java becomes

 obj.X = 10; 

However, behind the scenes, X is a method, not a variable, and therefore can do dirty input validation, etc.

+2
source

Of course, it has always been that the API often displayed read-only properties without the get prefix: String.length() and even the newer Buffer.capacity() are reasonable examples.

The surface of this is that there is less fluff. The disadvantage is that anything that tries to determine properties automatically based on conventions will not detect them. Personally, I tend to make mistakes aside from including the prefix.

Of course, in C # this is mostly irrelevant, because in any case there are "real" properties :)

+1
source

It depends. This is often redundant information, even in languages ​​with no properties.

In C ++, instead of the pair getAttr () / setAttr (), two overloads of the Attr () function are usually provided: void Attr (Foo f); // Foo Attr (); // Getter

Java usually uses the get / set prefix. I have to say that it’s best to go to which standard in your language. In Java, people expect to see get / set prefixes, so their absence can confuse people, although they are not strictly necessary.

+1
source

The C 2.0 lens also uses properties using the same point syntax.

Before that, he used a slightly different naming scheme for getters and setters (which, of course, can still be used with properties or for plain old attributes).

 value = [obj attr]; [obj setAttr:value]; [obj getAttr:&value]; 

That is, get is used differently. It does not return a value, but stores the result in the passed variable.

A typical getter has the same name as an attribute, setter is an attribute preceded by a set (according to the Java convention). These conventions are used by KVO (Key-Value Observation), so it should be followed.

0
source

Just a short addition: another convention is for getters of Boolean fields that must have the prefix "is" instead of "get", for example. bool isEnabled() { return enabled; }

0
source

I personally like the following rule:

  • Use the get prefix when the value is directly modified using the appropriate set method
  • Discourage get prefix in situations where a value is something that you cannot set directly as a property (i.e. there is no equivalent setXXX method)

The rationale for the second case is that if the value is not a user-defined "property" as such, then it does not need a pair of get / set methods. It is understood that if this convention is respected and you see the getXXX method, you can assume that the setXXX method exists.

Examples:

  • String.length() - since strings are immutable, length is a read-only value
  • ArrayList.size() - resize when adding or removing elements, but you cannot set it directly
0
source

All Articles