Avoid Internal Getters / Setters

In the source code of Activity.java, I see several ways:

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

and the definition of the getWindow method:

 public Window getWindow() { return mWindow; } 

But as the following rules:

Avoid Internal Getters / Setters

In native languages ​​such as C ++, it is a common practice to use getters (e.g. i = getCount ()) instead of directly accessing the field (i = mCount). This is a great habit for C ++, because the compiler can usually access, and if you need to limit or you can add code at any time.

On Android, this is a bad idea. Virtual method call methods are much more than an instance search field. It is 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 the fields directly.

Without JIT, direct access to fields is about 3 times faster than calling a trivial getter. With JIT (where direct access to a field is cheaper than access to a local one), a direct access field is about 7 times faster than causing a trivial getter. This is true in Froyo, but will improve in the future when JIT combines getter Methods.

so I want to know why Android developers do not have direct access to this mWindow object? If the JIT of current versions of Android cannot establish access, getWindow (). FindViewById (id) will cost more time than mWindow.findViewById (id), and findViewById is a fairly commonly used method.

+7
source share
3 answers

You cannot directly access the mWindow property - it is private . And I don't care about the speed of findViewById , since you only need to call it once for each view in the layout in your onCreate() method and save the views in members of your activity. You only call findViewById once to view it, right ?; -)

However, if you really care about these things, you can call getWindow() for yourself, save it in a local variable and call findViewById on it. I would not recommend this because all your performance improvements are not worth the time here and will be deprecated in future versions of JIT anyway.

If you do this, I will be very interested in how many microseconds you saved. :-)

+1
source

First: you cannot access it because it is closed.

Why is it closed?

As you said, direct access to members is faster. On the other hand, you call a method that is not very fast, because it will look for some kind of view hierarchy. Therefore, using the method instead of direct access will result in a small overhead as a percentage of the total time it takes to complete this task.

In any case, I believe that the reason for this is encapsulation.

You call what you don't have (this is the Android SDK). Thus, you should not make any assumptions about what is happening “on the other hand”. Just use this method and expect it to return the view you want (or null if it does not exist).

Perhaps the next version of android will use a different method to search for the view, and not to call getWindow() . If you use this method, they (Google / Android) can simply mark the method as deprecated and “forward” your call to the latest implementation. If you called getWindow() directly, you might be looking for something that is no longer hosted there.

+2
source

We have a reason to smile now ...

The Android documentation, which says to avoid internal getters and setters, will change soon, presumably, the program has been added to the Gingerbread platform, which copes with accessories expansion, please refer to “Avoid internal Getters / Setters” - these are bad tips and these two posts are SO.

0
source

All Articles