Object Orgy
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, an object orgy is a situation in which objects are insufficiently encapsulated via
information hiding In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
, allowing unrestricted access to their internals. This is a common failure (or
anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
) in
object-oriented design Object-oriented design (OOD) is the process of planning a Object-oriented programming, system of interacting objects for the purpose of solving a software problem. It is one approach to software design. Overview An Object (computer science), obje ...
or
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, and it can lead to increased maintenance needs and problems, and even unmaintainable complexity.


Consequences

The results of an object orgy are mainly a loss of the benefits of encapsulation, including: * Unrestricted access makes it hard for a reader to reason about the behaviour of an object. This is because direct access to its internal state means any other part of the system can manipulate it, increasing the amount of code to examine, and creating means for future abuse. * As a consequence of the difficulty of reasoning, design by contract is effectively impossible. * If much code takes advantage of the lack of encapsulation, the result is a scarcely maintainable maze of interactions, commonly known as a ''rat's nest'' or
spaghetti code Spaghetti code is a pejorative phrase for unstructured and difficult-to- maintain source code. Spaghetti code can be caused by several factors, such as volatile project requirements, lack of programming style rules, and software engineers with ins ...
. * The original design is obscured by the excessively broad interfaces to objects. * The broad interfaces make it harder to re-implement a class without disturbing the rest of the system. This is especially hard when clients of a class are developed by a different team or organisation.


Forms

Encapsulation may be weakened in several ways, including: * By declaring internal members public, or by providing free access to data via public
mutator method In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (together also known as accessors), which returns the value of the priva ...
s (setter or getter). * By providing non-public access. For example, see: Java access modifiers and ''accessibility levels'' in C#MSDN: Accessibility Levels, Visual Studio .NET 2003
). * In
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, via some of the above means, and by declaring
friend Friendship is a relationship of mutual affection between people. It is a stronger form of interpersonal bond than an "acquaintance" or an "association", such as a classmate, neighbor, coworker, or colleague. In some cultures, the concept of ...
classes or functions. An object may also make its internal data accessible by passing references to them as arguments to methods or constructors of other classes, which may retain references. In contrast, objects holding references to one another, though sometimes described as a form of object orgy, does not by itself breach encapsulation.


Causes

Members may be declared public to avoid the effort or syntactic overhead of providing proper accessors for them. This may increase readability of the class, but at the cost of the consequences described above. For some languages, a member intended to be readable by other objects can be made modifiable because the language has no convenient construct for read-only access. An object orgy may be a symptom of coding to an immature and
anemic Anemia or anaemia (British English) is a blood disorder in which the blood has a reduced ability to carry oxygen due to a lower than normal number of red blood cells, or a reduction in the amount of hemoglobin. When anemia comes on slowly, th ...
design, when a designer has insufficiently analysed the interactions between objects. It can also arise from laziness or haste in implementing a design, especially if a programmer does not communicate enough with a designer, or from reluctance to revise a design when problems arise, which also encourages many other anti-patterns. Many programmers view objects as anemic data repositories and manipulate them violating
Information Hiding In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
, Encapsulation and Design by Contracts principles.


Solutions

In general, encapsulation is broken because the design of other classes requires it, and a redesign is needed. If that is not the case, it may be sufficient to re-code the system according to best practices. Once the interfaces are published irrevocably, it may be too late to fix them.


References

{{Reflist


External links


PerlDesignPatterns.com
Anti-patterns