Bertrand Meyer’s Uniform Access Principle

Abel Rincón
3 min readApr 13, 2023

--

Image by vectorjuice on Freepik

In my first Medium article, I have decided to start with one of the classics of software design: Bertrand Meyer’s Uniform Access Principle. This fundamental concept, found in the book “Object-Oriented Software Construction” (1997), has been crucial for modern software development and has influenced a generation of developers. Below, we will analyze the importance of the uniform access principle and how it can be applied in building easy-to-maintain software.

Bertrand Meyer: A Pioneer in Software Design

Bertrand Meyer is an influential researcher, writer, and consultant in the field of computer languages, creator of the Eiffel programming language. His work has been essential in establishing the foundations of modern software design and has influenced a generation of developers.

Meyer’s Uniform Access Principle

The principle states that an object’s properties and operations should be accessible uniformly, without revealing whether they are stored attributes or results of operations. This allows clients of an object to focus on its functionality and behavior, rather than worrying about its implementation.

Example that violates the principle

To better understand this principle, it is helpful to see an example that does not comply with it. Consider the Square class representing a square in a two-dimensional plane. This class has a public $sideLength property and two public methods getArea() and getPerimeter() that calculate the area and perimeter of the square, respectively.

class Square {
public $sideLength;

public function __construct($sideLength) {
$this->sideLength = $sideLength;
}

public function getArea() {
return pow($this->sideLength, 2);
}

public function getPerimeter() {
return 4 * $this->sideLength;
}
}

In this example, it shows how you can access the $sideLength property directly, which violates the Uniform Access Principle and exposes the internal implementation of the class to external code. This can cause maintainability and flexibility issues.

Practical Example in PHP

To remedy this problem, the principle can be applied using getter and setter methods to interact with the object’s attributes and operations uniformly. For example, here is a new version of the Square class that complies with the principle:

class Square {
private $sideLength;

public function __construct($sideLength) {
$this->sideLength = $sideLength;
}

public function getSideLength() {
return $this->sideLength;
}

public function getPerimeter() {
return 4 * $this->getSideLength();
}

public function getArea() {
return pow($this->getSideLength(), 2);
}
}

In this example, the getSideLength method has been created for the $sideLength property. By doing this, it ensures that changes in the internal implementation of the class do not affect external users.

To demonstrate the flexibility of the principle, we can modify the Square class to store the perimeter instead of the side length:

class Square {
private $perimeter;

public function __construct($sideLength) {
$this->perimeter = $sideLength * 4;
}

public function getSideLength() {
return $this->getPerimeter() / 4;
}

public function getPerimeter() {
return $this->perimeter;
}

public function getArea() {
return pow($this->getSideLength(), 2);
}
}

In this version of the Square class, the property that stores the information has been swapped. Now the perimeter is stored instead of the side length,and the getter method has been modified to return the correct value. Despite these internal changes in the class implementation, clients can continue interacting with the class uniformly through the getter and setter methods, without being aware of the internal changes. This demonstrates the flexibility and ease of maintenance that the principle provides.

Risks of Misusing Getters and Setters

It is important to remember that excessive use of getters can lead to software design problems and violate the principle of encapsulation. Therefore, it is essential to follow the “tell, don’t ask” principle when designing classes and objects to provide methods that perform the desired task instead of accessing an object’s internal information.

In my next article, I will delve deeper into the “tell, don’t ask” principle and the risks of misusing getters.

Conclusion

The Uniform Access Principle is a fundamental concept in software design that has been key to the development of software. By applying this principle, more efficient, easy-to-maintain, and flexible software can be created.

In future articles, I will continue sharing information and learnings about classic software design concepts that may be useful and interesting.

--

--