Object orgy

In computer programming, object orgy is a term, common in the Perl programming community, describing a common failure (or 'anti-pattern') in object-oriented design or programming. In an object orgy, objects are insufficiently encapsulated, 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:

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.

External links

This article is issued from Wikipedia - version of the Friday, December 13, 2013. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.