ARCHIVE / How does one minimize closure errors when programming?

The simplest way to minimize closure errors when programming is to type all set definitions from the outside in.  For example, let's say that I wish to form an algebraic expression, such as (((A + B) / (C + D)) - ((F + G) * E)) + H.  If I was to type the expression from left to right, as I would if I was writing a sentence in English, I would have to predetermine the type and number of set closures before I could begin typing.  In many cases, doing so can be unmanageable or error-prone.  Subsequently, it is more effective to write the set definition by first defining the outside.  In this case, we would know from the outset that we are adding a subexpression to H.  So, start by typing "() + H".  Then backtrack to fill in the subexpressions, noting that you'll be subtracting two subexpressions, yielding "(() - ()) + H".  Backtrack again to fill in each incomplete subexpression, yielding "((() / ()) - (() * E)) + H", and repeat as needed to eventually yield the fully formed expression, "(((A + B) / (C + D)) - ((F + G) * E)) + H".

This method works just as well with other set definitions, such as nested blocks of code, strings of text and XML tags.  For example, if the desired result is:

if(a_value == true) {
if(another_value == 1) {
do=something;
}
else if(another_value == 2) {
check=something;
do=something_else;
}
else {
do=default_behavior;
}
}

The approach is to start by defining the outer code blocks:

Step 1

if() {

}

Then backtrack to define the nested code blocks from outer to inner.

Step 2

if(a_value == true) {
if() {

}
else if() {

}
else {

}
}

Step 3

if(a_value == true) {
if(another_value == 1) {
do=something;
}
else if(another_value == 2) {
check=something;
do=something_else;
}
else {
do=default_behavior;
}
}

This approach is certainly not new and has been integrated into many code editors that provide either contextual prefilling and/or bracket matching.

last updated 2008.03.13

RELATED PAGES

What is a programming closure error?

RELATED TOPICS

 

Terms of Use · Privacy Policy ©2017