请输入您要查询的百科知识:

 

词条 Pyramid of doom (programming)
释义

  1. Examples

  2. See also

  3. References

{{technical|date=February 2017}}

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 related 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

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 assumptions is wrong, 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

}

Or alternately:

if !windows.contains("Main") {

} else if !windows("Main").views.contains(5) {

} else {

    theWidth = windows("Main").views(5).size().width();    //more code that works with theWidth

}

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]
  • Law of Demeter
  • Safe navigation operator, a programming language operator that lets one avoid the pyramid of doom

References

1. ^{{Cite web|title=Why coroutines won’t work on the web |author=Dave Herman |date=14 December 2011 |website=The Little Calculist |url=http://calculist.org/blog/2011/12/14/why-coroutines-wont-work-on-the-web/ |archiveurl=https://web.archive.org/web/20160306010725/http://calculist.org/blog/2011/12/14/why-coroutines-wont-work-on-the-web/ |archivedate=2016-03-06 |dead-url=no}}
2. ^{{cite web |url=http://tritarget.org/blog/2012/11/28/the-pyramid-of-doom-a-javascript-style-trap/ |title=The Pyramid of Doom: A javaScript Style Trap |date=27 November 2012 |archiveurl=https://web.archive.org/web/20151209151711/http://tritarget.org/blog/2012/11/28/the-pyramid-of-doom-a-javascript-style-trap |archivedate=2015-12-09}}
3. ^{{cite web |url=http://blog.scottlogic.com/2014/12/08/swift-optional-pyramids-of-doom.html |title=Tearing Down Swift's Optional Pyramid Of Doom |first=Colin |last=Eberhardt |date=8 December 2014 |archiveurl=https://web.archive.org/web/20160731194838/http://blog.scottlogic.com/2014/12/08/swift-optional-pyramids-of-doom.html |archivedate=2016-07-31 |dead-url=no}}
4. ^{{cite web |url=http://blogs.msdn.com/b/vbteam/archive/2014/12/09/new-language-features-in-visual-basic-14.aspx |title=New Language Features in Visual Basic 14 |date=9 December 2014 |archiveurl=https://web.archive.org/web/20141225220053/http://blogs.msdn.com/b/vbteam/archive/2014/12/09/new-language-features-in-visual-basic-14.aspx |archivedate=2014-12-25 |dead-url=no}}
5. ^{{cite web |url=https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html |title=Optional Chaining |website=Apple}}
6. ^{{cite web |url=https://msdn.microsoft.com/en-us/library/Dn986595.aspx |title=Null-conditional Operators (C# and Visual Basic) |website=Microsoft}}
7. ^{{cite web |url=https://msdn.microsoft.com/en-us/library/hh156499.aspx |title=What's New for Visual C#|website=Microsoft}}
8. ^{{cite web | url=https://msdn.microsoft.com/en-us/library/we86c8x2.aspx|title=What's New for Visual Basic|website=Microsoft}}
9. ^{{cite web|title=What's The Point Of Promises?|date=March 28, 2013|author=Joe Zimmerman|website=telerik.com|url=http://www.telerik.com/blogs/what-is-the-point-of-promises}}

1 : Programming constructs

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 23:54:37