Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid Internal Getters/Setters

Tags:

android

mobile

In the source code of Activity.java, I see some methods bellow :

public View findViewById(int id) {
    return getWindow().findViewById(id);
}

and the definition of getWindow method:

public Window getWindow() {
    return mWindow;
}

But as the following rules:

Avoid Internal Getters/Setters

In native languages like C++ it's common practice to use getters (e.g. i = getCount()) instead of accessing the field directly (i = mCount). This is an excellent habit for C++, because the compiler can usually inline the access, and if you need to restrict or debug field access you can add the code at any time.

On Android, this is a bad idea. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.

Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter. This is true in Froyo, but will improve in the future when the JIT inlines getter methods.

so I want to know why android developers not access this mWindow object directly? If the JIT of the current android versions cannot inline the access, getWindow().findViewById(id) will costs more time than mWindow.findViewById(id), and findViewById is a rather frequently used method.

like image 691
teoking Avatar asked Dec 27 '10 08:12

teoking


People also ask

How do you avoid getters and setters?

Thus: you avoid getters and setters by thinking in terms of behavior, not in terms of state. Getters/setters manipulate state, from the "outside" (by doing avail = purse.

Should you always have getters and setters?

Using getters and setters, is always, in my opinion good practice. One thing you should avoid is to have external entities mess with the internal structure of your class at will. Typical example, consider having a dateOfBirth parameter.

Can we use getters without setters?

Depends on if the value you are talking about is something you want to let other classes modify - in some cases the answer is yes, in some it is no. If the answer is no then there is no reason to add a setter method and in fact it might harm things.

How do you avoid getters and setters in spring boot?

You can use Lombok . It is a small java library that can be used to reduce the amount of boilerplate java code. Lombok does this via annotations that can be added to the java classes. For getters/setters Lombok provides @Getter and @Setter annotations.


2 Answers

First: you can't access it because it's private.

Why is it private?

As you said, accessing members directly is faster. On the other hand, you are invoking a method that isn't very fast as it will lookup for some view in the view hierarchy. So using a method instead of a direct access will incur in a small overhead in terms of percentage of the total time that it would take to perform that task.

Anyway, I believe that the reason for this is encapsulation.

You are invoking something you don't own (that is the Android SDK). So, you shouldn't make any assumptions of whats happening "in the other side". Simply use this method and expect that it will return the view you want (or null if it doesn't exists).

Maybe the next version of android will use a different method to lookup a view, not calling getWindow(). If you use this method, they (Google/Android) can simply mark the method as deprecated and "forward" your invocation to the newest implementation. If you were calling directly getWindow(), maybe you would be looking for something that is no longer placed in there.

like image 141
Pedro Loureiro Avatar answered Sep 19 '22 15:09

Pedro Loureiro


You can't access the mWindow property directly - it's private. And I wouldn't care about the speed of findViewById, since you only need to call it once for every view in your layout in your onCreate() method and store the views in members of your activity. You do call findViewById only once per view, don't you? ;-)

However, if you really care about these things, you could call getWindow() for yourself, store it into a local variable and call findViewById on it directly. I wouldn't recommend this because all your performance increasements here are not worth the time and anyway will be obsolete with future versions of the JIT.

If you do this I would be very interested in the amount of microseconds you saved. :-)

like image 44
mreichelt Avatar answered Sep 20 '22 15:09

mreichelt