Uniform access principle

From Wikipedia, the free encyclopedia

The Uniform Access Principle was put forth by Bertrand Meyer. It states "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." This principle applies generally to object-oriented programming languages. In simpler form, it states that there should be no difference between working with an attribute, precomputed property, or method/query.

While most examples focus on the "read" aspect of the principle, Meyer shows that the "write" implications of the principle are harder to deal with in his monthly column on the Eiffel programming language official website.

Many languages have various degrees of support for UAP, where some of the implementations violate the spirit of UAP.

Contents

If a language allows access to a variable via dot-notation and assignment

Foo.bar = 5 //Assigns 5 to the object variable "bar"

then these operations should be the same :

//Assume print displays the variable passed to it, with or without parens
//Assume Foo.bar = 5 for now
print Foo.bar
print Foo.bar()

When executed, should display :

5
5

This allows the object to still hide information as well as being easy to access.

The same should be true for setting the data.

Foo.bar = 5
Foo.bar(5)
//These should achieve the same goal

Ruby programming language :

class Foo
  attr_reader :x
  def initialize(x)
    @x = x
  end
  def x_times_5
    return @x*5
  end
end

y = Foo.new(2)
puts y.x
puts y.x_times_5

This outputs:

2
10

Note how even though x is an attribute and x_times_5 is a parameterless method call, they're accessed the same way.

Python programming language

class Foo(object):
    def __init__(self, x):
        self.setx(x)

    def getx(self):
        return self.__x

    def setx(self, x):
        self.__x = x

    def getx_times_5(self):
        return self.x*5
    
    x = property(getx, doc="getter for x")
    x_times_5 = property(getx_times_5, doc="getter for x, times 5")

y = Foo(2)
print y.x
print y.x_times_5

This outputs:

2
10

Python properties can be used to achieve UAP. It should be noted that this method of using properties to map other functions to variable access matches Meyer's idea of UAP closely (Eiffel uses a similar mechanism).


Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.