Pyramid of doom (programming)

A failure at any of these steps means that the write operation cannot be completed and an error should be returned to the calling program.

If many tests are required, the enclosed blocks of code can march off the page to the right margin.

For example, the pyramid of doom is commonly seen when checking for null pointers or handling callbacks.

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.

To avoid this error, the programmer has to check every method call to ensure it returns a value.

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).