Object orgy
Encyclopedia
In computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, object orgy is a term, common in the Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 programming community, describing a common failure (or 'anti-pattern
Anti-pattern
In software engineering, an anti-pattern is a pattern that may be commonly used but is ineffective and/or counterproductive in practice.The term was coined in 1995 by Andrew Koenig,...

') in object-oriented design or programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

. In an object orgy, objects are insufficiently encapsulated
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 decision is changed...

, allowing unrestricted access to their internals, usually leading to unmaintainable complexity.

Consequences

The consequences of an object orgy are essentially a loss of the benefits of encapsulation:
  • Unrestricted access makes it hard for the 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 be examined, and opening the door to future abuse.
  • As a consequence of the difficulty of reasoning, design by contract
    Design by contract
    Design by contract , also known as programming by contract and design-by-contract programming, is an approach to designing computer software...

     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
    Spaghetti code is a pejorative term for source code that has a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, or other "unstructured" branching constructs. It is named such because program flow tends to look like a bowl of spaghetti, i.e. twisted and...

    .
  • 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 the clients of the class are developed by a different team or organisation.

Forms

Encapsulation may be weakened by declaring internal members public or by providing free access to data via public getter/setter methods.
The access need not be public: for details see e.g. Java access modifiers and the Accessibility Levels in C# (MSDN).
In C++, encapsulation is also weakened by declaring friend-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.

On the other hand, objects holding references to one another, though sometimes described as a form of object orgy, does not in itself breach encapsulation.

Causes

Members may be declared public to avoid the effort or syntactic overhead of providing proper accessors for them. This may well increase readability of the class itself, but at the expense of the consequences described above.

In particular, a member intended to be readable by other objects may be made modifiable as well, because the language used does not provide a convenient construct for read-only access.

An object orgy may be a symptom of coding to an immature design, when the designer has not sufficiently analysed the interactions between objects.
It can also arise from laziness or haste in implementing the design, especially if the programmer does not communicate enough with the designer.
Reluctance to revise the design when problems arise also encourages this (and many other) anti-patterns.

Solutions

In general, encapsulation is broken because the design of other classes requires it, and a redesign is required.
If that is not the case, it may be sufficient to re-code the system according to best practices.
Once the interfaces have been irrevocably published, it may be too late to fix them.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK