encapsulation · language feature · API design · object collaboration design



-- Bertrand Meyer

Bertrand Meyer coined this principle in his highly-influential book Object-Oriented Software Construction.[1]


This gives the person object the flexibility to change between the two easily as well as removing what is usually an unnecessary concern from the client. It's an important part of encapsulation - or at least the data-hiding aspect of encapsulation.

Although it's a fundamental feature of object-oriented programming, remarkably few OO languages really follow it. (Eiffel is naturally an exception.) Most languages simulate it by convention, hence the habit of using accessors such as getAge in Java and C++ programs.

Whatever the language does, it's important that programmers don't violate the principle. Often you'll see conventions that subvert uniform access. An example of this might be saying that data accessors are named getAge while computed accessors are named calcAge. I make a point of naming my accessors the same way in either case.

One argument against uniform access is that data access is fast while computation can be slow - and it's useful to give client programmers an indication if a method is slow to respond. I prefer to treat this as a special case - follow the uniform access principle unless there is a particularly slow computation (and that should be one that's been measured to be slow, not just expected to be slow). And in that case there are other options, such as a caching accessor, that you can also explore.

Uniform access applies to both getters and setters, although often computed accessors are read-only. Whether a property is read-only or not is a separate issue as to whether it is stored or computed.


1: This formulation of of the principle is from the second edition. In the first edition he called it the Uniform Reference Principle (§2.1.4). He changed it to Uniform Access Principle in the second edition and supplied the quoted definition.