Pyramid of doom (programming)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

In computer programming, the pyramid of doom is a common problem that arises when a program uses many levels of nested indentation to control access to a function. It is commonly seen when checking for null pointers or handling callbacks.[1] Two examples of the term are used to a particular programming style in JavaScript,[2] and the nesting of if statements that occurs in object-oriented programming languages when one of the objects may be a null pointer.[3][4]

Examples

Object property dereferencing

Most modern object-oriented programming languages use a coding style known as dot notation that allows multiple method calls to be written in a single line of code, each call separated by a period. For instance:

    theWidth = windows("Main").views(5).size.width

This code contains four different instructions; it first looks in the collection of windows for a window with the name "Main", then looks in that window's views collection for the 5th subview within it, then calls the size method to return a structure with the view's dimensions, and finally calls the width method on that structure to produce a result that is assigned to a variable name theWidth.

The problem with this approach is that the code assumes that all of these values exist. While it is reasonable to expect that a window will have a size and that size will have a width, it is not at all reasonable to assume that a window named "Main" will exist, nor that it has five subviews. If either of those possibilities are true, one of the methods will be invoked on null, producing a null pointer error.

To avoid this error, the programmer has to check every method call to ensure it returns a value. A safer version of the same code would be:

    if windows.contains("Main") {
        if windows("Main").views.contains(5) {
            theWidth = windows("Main").views(5).size.width
            //more code that works with theWidth
        }
    }

If the programmer wishes to use that value based on whether or not it exists and is valid, the functional code inside the if statements is all pushed to the right, making it difficult to read longer lines. This often leads to attempts to "flatten" the code:

    if windows.contains("Main") { theWindow = windows("Main") }
    if theWindow != null && theWindow.views.contains(5) { theView = theWindow.views(5) }
    if theView != null {
        theWidth = theView.size.width
        //additional code
    }

This sort of programming construct is very common and a number of programming languages have added some sort of syntactic sugar to address this. For instance, Apple's Swift added the concept of optional chaining in if statements[5] while Microsoft's C# 6.0 and Visual Basic 14 added the null-conditional operators ?. and ?[ for member access and indexing, respectively.[6][7][8] The basic idea is to allow a string of method calls to immediately return null if any of its members is null, so for instance:

    theWidth = windows("Main")?.views(5)?.size.width;

would assign null to theWidth if either "Main" or the fifth subview is missing, or complete the statement and return the width if they are both valid. There are many times where the programmer wants to take different actions in these two cases, so Swift adds another form of syntactic sugar for this role, the if let statement, also known as "optional binding":

    if let theView = windows("Main")?.views(5) {
        //do things knowing the view exists...
        theWidth = theView.size.width
    }

See also

  • Promises, a technique for avoiding the pyramid of doom, e.g. used in JavaScript[9]

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.