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:
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 assumptions is wrong, the corresponding call will result in 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 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:
Or alternatively:
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 statements5 while Microsoft's C# 6.0 and Visual Basic 14 added the null-conditional operators ?. and ?[ for member access and indexing, respectively.678 JavaScript added support for the optional chaining operator in 2020.9 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:
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":
Pyramid of Doom can usually be resolved in any language by simply breaking up the code into multiple nested functions (or other groupings). For instance, instead of:
You can break up the functionality like this:
Similarly, data structures can be broken up by levels, when similar pyramids occur.
Not only is the Pyramid of Doom solved, but it's better practice to not have large, complicated functions; smaller ones are easier for a programmer to write without erring, easier to read, and easier to verify. Choosing function names for each of these levels will also help the author clarify to readers what is done where. Typically, each level doesn't need many connections to levels that are far away, so separating them out is easy. If there are such connections, the author can re-think their design to something more reliable, because this is a fertile source of bugs.
Dave Herman (14 December 2011). "Why coroutines won't work on the web". The Little Calculist. Archived from the original on 2016-03-06. http://calculist.org/blog/2011/12/14/why-coroutines-wont-work-on-the-web/ ↩
"The Pyramid of Doom: A javaScript Style Trap". 27 November 2012. Archived from the original on 2015-12-09. https://web.archive.org/web/20151209151711/http://tritarget.org/blog/2012/11/28/the-pyramid-of-doom-a-javascript-style-trap ↩
Eberhardt, Colin (8 December 2014). "Tearing Down Swift's Optional Pyramid Of Doom". Archived from the original on 2016-07-31. http://blog.scottlogic.com/2014/12/08/swift-optional-pyramids-of-doom.html ↩
"New Language Features in Visual Basic 14". 9 December 2014. Archived from the original on 2014-12-25. http://blogs.msdn.com/b/vbteam/archive/2014/12/09/new-language-features-in-visual-basic-14.aspx ↩
"Optional Chaining". Apple. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html ↩
"Null-conditional Operators (C# and Visual Basic)". Microsoft. 7 March 2024. https://msdn.microsoft.com/en-us/library/Dn986595.aspx ↩
"What's New for Visual C#". Microsoft. 21 May 2024. https://msdn.microsoft.com/en-us/library/hh156499.aspx ↩
"What's New for Visual Basic". Microsoft. 21 February 2023. https://msdn.microsoft.com/en-us/library/we86c8x2.aspx ↩
"Optional chaining in JavaScript". 28 October 2024. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining ↩
Joe Zimmerman (March 28, 2013). "What's The Point Of Promises?". telerik.com. http://www.telerik.com/blogs/what-is-the-point-of-promises ↩