Real estate wasted by indent

Indentation is a nice way to show the structure. But note that you can not write anything in the space wasted by the indentation. Your precious writing space comes after indentation. So you should use minimum indentation.

For example, I think the following way is bad because the real work is indented.

bad:

    int result = -1;

    while (result < 0) {
        // Do the real work here.
    }

The real work would be several lines long, and include complex math. So we want to give it as much space as possible.

good:

    int result = -1;

  retry:
    // Do the real work here.

    if (result < 0) goto retry;

You say goto is bad? I don’t think so. In this kind of case, retry is only an exceptional path, and looping around is not important point of  the work. Using while() is rather misleading and overkill.

Advertisements