How is the new property class in API14 better than internal getters / setters?

With their release of 4.0, they introduce a new class of properties that helps you in general to set or get internal values ​​for an object. Described here (scrolls a bit): http://developer.android.com/sdk/android-4.0.html#api and is defined here: http://developer.android.com/reference/android/util/Property.html

At the same time, they say to avoid internal getters / setters. http://developer.android.com/guide/practices/design/performance.html#internal_get_set

Am I missing something regarding how the Property class does not violate this rule? I understand that this is useful in coding, but not in performance.

Thoughts?

+4
source share
1 answer

Am I missing something regarding how the Property class does not violate this rule?

He "breaks" the rule. However, the rule is not universal, because the document you are quoting is making great efforts to try to explain from the very beginning. This document describes "microoptimization." They, as stated in the document, "will almost never do or violate your software." Rather, these are things that you might want to optimize in certain places that need it.

You do not need to avoid internal getters and setters unless they cause special performance problems . You can usually determine this using Traceview and seeing a lot of time spent in getters and setters.

So, for example, in a narrow loop over a large data set, getters and setters should probably be avoided. Outside of scenarios like this, using getters and setters is unlikely to cause you any kind of material pain or user. Again, let tools like Traceview be your guide.

Now, oddly enough, some of the intended uses of the Property (such as ObjectAnimator ) may seem more demanding on micro-optimization than your average code. I think you just need to see if the animation works as smoothly as you want. I have not used ObjectAnimator directly yet, not to mention the ICS Property , so I can only hope that all of this will work well.

+2
source

Source: https://habr.com/ru/post/1379753/


All Articles