In programming, various terms like abstraction are mentioned quite heavily. What, however, is composition? What is a basic but effective example of composition at play to result in good quality code (good quality meaning easy to understand, short and concise, etc).

  • Are you asking about class composition, functional composition, or something else? – Caleb Sep 14 '11 at 23:25
  • I mean functional composition... – GurdeepS Sep 17 '11 at 1:57

In the functional programming sense, a composition is:

if a function f takes a foo and returns a bar, and if a function g takes a bar and returns a blah, then there is a function h which takes a foo and returns a blah, such that: h(foo) = g(f(foo))

That is, if f is "take the length of a string" and g is "double the size of an integer" then there is a composed function h that takes the length of a string and doubles it.

In the object oriented programming sense a composition is: a class C may contain fields of classes D and E. C is a class which composes a D with an E.


Composition is simply when you have multiple objects compose one more complex object. Example:

class Person {
    string FirstName { get; set; }
    string LastName { get; set; }

Person is composed of a FirstName and LastName


It depends what you mean by composition, which is used for various things. I first think of the composite pattern, which is widely used. Handling data in a tree structure is a simple example of a composite pattern, and allows easy recursion through the nodes.


In OO, there are at least these meanings of Composition in addition to what was mentioned before:

1-Composition of different data types in a class represents a new type.

Class Household { public car myCar; public oven myOven; public tv myTv; }

2-Composition as a type of association between 2 objects composition in UML is reppresented using the "has a" association label and is represented by black-filled diamond shape. See:



3-Inner classes represent a type of composition. In C#.Net you can have 1 class code file having more than 1 class definition inside it.

In Entity Relationship Modeling, composition is implemented by including the primary key of the parent table as part of the key of the primary key of the child table and establishing a constraint such that when the parent is deleted, all children are deleted.

This representation, toget*strong text*her with composition in object association results in lif-time strong bond between components so that no one can live (or have a business meaning) without the other. A good example may be an invoice that is composed of header and detail lines. The detail lines by themselves are meaningless without the header information and vice verse.


Although the term composition can refer to many aspects of programming such as multiple objects within a class, the term when speaking about abstract programming typically means executing multiple functional lines of code within a single statement. Think of f(g(x)) in mathematical terms.

The most common method of composition I can think about in real-world programming is defining pre-processor directives in say C++ or C.

 #define getmax(a,b) a>b?a:b

the above defines an if/then function returning max of 2 different integers that can then be used within other lines of code thus creating a composition.

If you have ever heard the term public and private keys when speaking of cryptography, this is a classic example of composition algorithms where f(g(x)) and x is typically your private key.

  • 1
    The answer is correct, but never use #define statements in that way! You can get unexpected results just by running the following line of code: int c = getmax(++a,b);. (If you can't see why that breaks, run it using different values for a and b and see what happens to c). Use inline functions instead. – riwalk Sep 14 '11 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.