Pyramid of doom (programming): Difference between revisions

Content deleted Content added
old
m If windows("Main") is null, .views() will be called on null--better just to be general I expect :3
Line 13:
first looks in the array of windows for a window with the name "Main", gets the 5th subview within it, then calls the <code>size</code> method to return a structure with the view's dimensions, and finally calls the <code>width</code> method on that structure to produce a result that is assigned to <code>theWidth</code>.
 
The problem with this approach is that the code assumes that all of these values exist. While it is reasonable to suggest 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 <code>size</code>methods will be calledinvoked 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: