24

Just read this on the dev site:

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.

Update in 2019/05: In the current version, the above material has been removed from the document!

So is it saying you would use field access within the class:

 public class MyObject {

    public Object innerObject; // This would be private if I was using a getter

    public void doSomeStuff(){
          if(innerObject){        // Within class access like this
                 // ....
          }
    }

    public Object getInnerObject(){  // This would be removed if I was using field access
         return innerObject;
    }
 }

But what about access from another object?:

 public class SecondObject {

      public void doSecondSomething(){
                MyObject ob = new MyObject();
                Object inner;

                //This is my question basically (from an Android performance perspective)   
                inner = ob.getInnerObject();
                // OR 
                inner = b.innerObject

       }

 }
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Just to point this out, remember that Dalvik isn't JVM and this practice shouldn't be taken as a general way to do in normal Java development. – Esko Jul 16 '11 at 09:43
  • 1
    Your quote says it all, doesn't it? **It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface**. Also: don't go for over-design. Just use the getter unless you do a major amount of calls. or have a speed problem. – Nanne Jul 16 '11 at 10:16

7 Answers7

32

The performance hit of using internal getters and setters also applies to external getters and setters.

However, in the external case the getters and setters have significant benefits in other areas; e.g. preserving encapsulation, reducing harmful coupling, making your code more maintainable, and so on. So, it is generally regarded as best practice to use getters and setters despite the performance hit that this may incur.

The performance hit is a result of limitations of older Android JIT compilers. This situation improved significantly with Gingerbread (see - https://stackoverflow.com/a/4930538/139985 ... and note who wrote that answer!) and continues to improve. Indeed, in the current (2019) version of the Performance Tips, the entire section advising about internal getters and setters has been removed.

In general, it is a bad idea to "tune" your code for an inferior platform, especially if there is a reasonable chance that a better one is in the offing.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Wait, the android JIT doesn't inline trivial function calls? I mean that's one of THE basic optimizations for any JIT - sure it has to make sure that the class wasn't overwritten and so on, but that's in 99/100 easy. Hard to believe. – Voo Jul 16 '11 at 12:04
  • 1
    @Voo - Business reasons may make it necessary to deliver something 2nd rate now than wait 6 months until it is 1st rate. The same thing happened with Java and Javascript (and Windows Vista, and ...). It is only "hard to believe" if you ignore history. – Stephen C Jun 04 '12 at 00:59
4

Although b.innerObject is faster, as the technology advances (better cpus, JIT, etc) the difference between the two options gets smaller.

The only point where it may matter is when done in intensive loops that are executed all the time. For example, in the onDraw method of a game, when you loop through hundreds of objects.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
3
// this is faster
inner = b.innerObject

// but this won't hurt performance much because
// it's assumed that it will be rare
inner = ob.getInnerObject();
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Forrest Voight
  • 2,249
  • 16
  • 21
3

Keep in mind that those performance considerations are relevant only if the member in question is accessed thousands of times per second.

A good example where direct access may be a good idea is the scenegraph of a game (libgdx)

public abstract class Actor {
    public Group parent;
    public final String name;
    public boolean touchable = true;

    public float x;
    public float y;
    public float width;
    public float height;
    public float originX;
    public float originY;
    public float scaleX = 1;
    public float scaleY = 1;
    public float rotation;
    public final Color color = new Color(1, 1, 1, 1);
mibollma
  • 14,959
  • 6
  • 52
  • 69
2

Getters and Setters always have an overhead as they are function calls. When you are in the same object you can optimize your code by not using them as you know what they are used for and you don't need to abstract/encapsulate them from its own object.

I think you also need to look at it from a different perspective:

  1. Will a not having a getter/setter break common oops practices ? You will not want to have direct references if you are making objects/modules that others will use.

  2. You really don't want to use getters/setters too many times as in the end , unless the sh!* its optimized out of it function calls will have overhead.

You really need to optimize on a percase basis , if I am building two modules where some components are only accessed by each other I might make a static field else I will stick to getters/setters

Ravi Vyas
  • 12,212
  • 6
  • 31
  • 47
  • 1
    "Getters and Setters always have an overhead as they are function calls" not true on any modern JIT - in Hotspot that would be inlined, except in some rare cases (ie the setter is really overwritten in a subclass AND the jit can't assure one of the identity of the object - or otherwise said, almost never) – Voo Jul 16 '11 at 12:05
2

Performance wise, there is no difference in accessing this.field or that.field.

The feeling that an instance field is more accessible to the object hosting it is just a syntax illusion.

OO wise, seriously, how complex can an Android app become? Many of the OO mantras are from building monster apps. What's the big deal if your little app uses objects like structs?

And even in a huge app, as long as it's in house, and all source code accessing a field are available for refactoring, there is no problem at all exposing the fields.

irreputable
  • 44,725
  • 9
  • 65
  • 93
0

For the record, another problem with setter and getter (in Java) is they are painful ugly to use.

Let's say the next exercise, we need to modify a field insider a field of a object

In java is:

   object.getField().getSubField().setField3("hello"); // ugly

While in C# the same code is (even with encapsulation)

   object.Field.SubField.Field3="hello";  // fine

So, using public field in Java (or android) is a lot cleaner :

  object.field.subfield.field3="hello"; // fine too
magallanes
  • 6,583
  • 4
  • 54
  • 55
  • 1
    ah but this is the TrainWreck antipattern, only talk to your nearest neighbour and have the code `object.setField3("hello");` http://c2.com/cgi/wiki?TrainWreck – Blundell Mar 22 '16 at 12:25
  • 1
    I find getter/setter paradigm is way over sold. 99% of the time they are just pass-through to the member variable. If you plan to implement them both, just skip the getter/setter and make the variable public. Engineers constantly quote good encapsulation (or more specifically isolation) as the primary benefit to getter/setter. The code gets bound to the syntax of your getter/setter which is identically to getting bound to the class member variable. So no gain. The gain is if you change implementation in the future or derived class overloads. When that occurs make the getter/setter – LanDenLabs Sep 02 '17 at 17:45
  • @user1959190 Exactly. However, i disagree with the comment about engineers. A true engineer focuses in efficiencies and results. In fact, a seasoned engineer cheats a lot. However, there are so many programmers that are not engineers but more close to scientists. I use setter and getter because its the standard and teamwork. However, i cringe because its useless in 99% of the time, and when its useful then it hides implementation and its really dangerous. – magallanes Sep 02 '17 at 21:23
  • A good example of not using getter/setter is the Android LatLng class https://developers.google.com/android/reference/com/google/android/gms/maps/model/LatLng – LanDenLabs Sep 02 '17 at 21:55
  • I also agree so lets change the group from engineers to developers. – LanDenLabs Sep 02 '17 at 21:57