Circular dependency

Circular dependency

In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly.

Contents

Overview

Circular dependencies are natural in many domain models where certain objects of the same domain depend on each other. However, in software design circular dependencies between larger software modules are considered an anti-pattern because of their negative effects.

Problems of circular dependencies

Circular dependencies can cause many unwanted effects in software programs. Most problematic from a software design point of view is the tight coupling of the mutually dependent modules which reduces or makes impossible the separate re-use of a single module.

Circular dependencies can cause a domino effect when a small local change in one module spreads into other modules and has unwanted global effects (program errors, compile errors). Circular dependencies can also result in infinite recursions or other unexpected failures.

Circular dependencies may also cause memory leaks by preventing certain very primitive automatic garbage collectors (those that use reference counting) from deallocating unused objects.

Causes and solutions

In very large software designs, software engineers may lose the context and inadvertently introduce circular dependencies. There are tools to analyze software and find unwanted circular dependencies[1][2].

Circular dependencies are often introduced by inexperienced programmers who need to implement some kind of callback functionality. Experienced programmers avoid such unnecessary circular dependencies by applying design patterns like the observer pattern.

Example of circular dependencies in C++

Implementation of circular dependencies in C/C++ can be a bit tricky, because any class or structure definition must be placed above its usage in the same file. A circular dependency between classes A and B will thus both require the definition of A to be placed above B, and the definition of B to be placed above A, which of course is impossible. A forward declaration trick is therefore needed to accomplish this.

The following example illustrates how this is done.

  • File a.h:
#ifndef A_H
#define A_H
 
class B;      //forward declaration
 
class A {
public:
        B* b;
};
#endif //A_H
  • File b.h:
#ifndef B_H
#define B_H
 
class A;      //forward declaration
 
class B {
public:
        A* a;
};
#endif //B_H
  • File main.cpp:
#include "a.h"
#include "b.h"
 
int main() {
        A a;
        B b;
        a.b = &b;
        b.a = &a;
}

Note that although a name (e.g. A) can be declared multiple times, such as in forward declarations, it can only be defined once (the One Definition Rule).

Self-reference example

Following is another example of forward declaration, which might be useful if the application needs a self-sustaining array of objects which is able to add and remove objects from itself during run-time:

  • File a.h:
class A {
public:
    static A *first, *last;
    A *previous, *next;
 
    A();
    ~A();
};

The static variables first and last have to be defined, because their declaration does not reserve memory space for them. Note: static variables do not change from object to object and stay the same for this given class.

They should also be initialized to 0, or NULL, so we know what they are to start with.

  • File a.cpp:
#include "a.h"
 
A *A::first=0, *A::last=0; // don't put the word static here, that will cause an error
 
A::A() {
    if(first==0) first=this; //first A created
    previous=last;
    if(previous != 0) previous->next=this;
    last=this;
    next=0;
}
 
A::~A() {
    if(previous != 0) previous->next=next;
    if(next != 0) next->previous=previous;
}

References

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Dependency hell — is a colloquial term for the frustration of some software users who have installed software packages which have dependencies on specific versions of other software packages.[1] This was mainly attributable to old Linux package managers. Current… …   Wikipedia

  • Circular — is a basic geometric shape such as a Circle. Contents 1 Documents 2 Travel and transportation 3 Places …   Wikipedia

  • Dependency graph — In mathematics, computer science and digital electronics, a dependency graph is a directed graph representing dependencies of several objects towards each other. It is possible to derive an evaluation order or the absence of an evaluation order… …   Wikipedia

  • Media-system dependency — Media System Dependency, first introduced by Ball Rokeach and DeFleur (1976), is defined as “a relationship in which the capacity of individuals to attain their goals is contingent upon the information resources of the media system.” Those… …   Wikipedia

  • Postage stamps and postal history of the Ross Dependency — Ross Dependency stamps have been issued by New Zealand postalauthorities for use on mail from Scott Base since 1957.Overprinted New Zealand stamps had been used for mail on two earlierexpeditions to the region.King Edward VII LandBefore leaving… …   Wikipedia

  • Domain Name System — The Domain Name System (DNS) is a hierarchical distributed naming system for computers, services, or any resource connected to the Internet or a private network. It associates various information with domain names assigned to each of the… …   Wikipedia

  • Anti-pattern — For the book, see AntiPatterns. In software engineering, an anti pattern (or antipattern) is a pattern that may be commonly used but is ineffective and/or counterproductive in practice.[1][2] The term was coined in 1995 by Andrew Koenig,[3]… …   Wikipedia

  • Economy of Djibouti — As such, Djibouti’s economy is dominated by the services sector, providing services as both a transit port for the region and an international transshipment and refueling center.From 1991 to 1994, Djibouti experienced a civil war that had… …   Wikipedia

  • DNS root zone — A DNS root zone is the top level DNS zone in a Domain Name System (DNS) hierarchy. Most commonly it refers to the root zone of the largest global DNS, deployed for the Internet. Ultimate authority over the DNS root zone rests with the US… …   Wikipedia

  • C Sharp (programming language) — The correct title of this article is C# (programming language). The substitution or omission of the # sign is because of technical restrictions. C# Paradigm(s) multi paradigm: structured, imperative …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”