CSC/ECE 517 Fall 2009/wiki3 19 ee

From Expertiza_Wiki
Jump to navigation Jump to search

Uniform Access Principle

The Uniform Access Principle states that any attribute of a module (usually a class) should be accessed with the same syntax, regardless of the nature of the data. Simply put, when a consumer reads or writes a value, they should not know (or care) weather they accessing a variable or invoking a method. Thus, no matter how the module internally handles the data, externally the information is always accessed uniformly.

Origin of the Uniform Access Principle

The Uniform Access Principle was created by the French engineer Bertrand Meyer. Meyer is known to be a strong proponent of object oriented programming, and in fact was one of the earliest and most outspoken champions of OO. His book Object-Oriented Software Construction, first published in 1988, is considered to be a foundational publication of the object oriented programming movement.[1] It was in this book that he put forth the uniform access principal by stating "All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation."[2]

Advantages of the Uniform Access Principle

There are many advantages associated with using the Uniform Access Principal when writing software.

Reading Properties

Ease of Understanding

Accessing data from a class can be confusing if the user does not know how the data is stored. For example, in C# it is often hard to remember how to access the number of items in a collection - is it collection.Count or collection.Count()? It turns out that the syntax is collection.Count for a generic collection, but it is collection.Count() for an enumerable collection.[3]

This difference in syntax can be confusing to a user trying to access the data within an otherwise opaque object. If this data was uniformly accessible, it would be much less confusing.

Convenience of Interface

Similarly, it is convenient for a user to not have to remember details of an objects implementation to access data within the object. If the Uniform Access Principal is exercised, the programmer does not need to keep track of which values are computed in methods and which are just attributes. A user can simply access the data named what they are interested in.

Maintainability of Interface

Utilizing the Uniform Access Principal also aids in maintaining (reads 'not breaking') existing interfaces. For example, lets say there was a BankAccount class. That BankAccount class has an attribute called "Balance", which simply contains the balance value for that account. But later, the policy of the BankAccount changes, such that the balance is not a stored value, but must be computed by adding multiple values.

In implementations that do not support the Uniform Access Principal, the accessing of that data could go from bankAcnt.Balance to bankAcnt.GetBalance(), thus 'breaking' any code that was depending on the balance property.

However, with an implementation that utilizes the Uniform Access Principal, this change will not be a problem. Be it a stored value, or a value computed in a method call, the accessing of this data could always be done with the same syntax.

Disadvantages of the Uniform Access Principle

Writing Properties

Performance Issues

Class Maintenance

Language Restrictions

Code Examples

References