[oclug] Evolution problem

Matthew Wilcox willy at debian.org
Tue Jun 17 15:48:43 EDT 2003


On Tue, Jun 17, 2003 at 03:04:24PM -0400, Jon Earle wrote:
> Well, I (perhaps naively) take issue with the C and 3 level indenting
> thing.  To start, I indent after the start of a function, giving a first
> level of indetation right off the bat.

> int myfunc() {
>     /* Everything is indented 1 tab char, which I've set in my vi to
>      * be 4 characters.  First level of indenting.
>      */
> 
>     while (1) {
>         /* Second level of indenting.
>          */
> 
>         for (i = 0; i < x; i++) {
>             /* Third level of indenting.
>              */
> 
>             if (a) {
>                 /* Fourth level of indenting.
>                  */
>                 if (a == b) {
>                     /* Fifth level of indenting.
>                      */
>                 }
>             } else {
> 
>             }
>         }
>     }
> }

> This code could do something like read lines from a file, then iterate
> through each token on the line searching for a valid, specific token on
> which to perform some action.  The code is readable and resonable, yet
> contains more than three levels of indentation.  As long as all the
> intermediary processing code is stripped away into supporting functions,
> this higher level function remains tight and readable.
> 
> Interested in thoughts on this.

It's certainly possible to write code like this.  but look how much more
legible it is to write:

void frob_my_socks(void)
{
	if (a) {
		if (a == b) {
		}
	} else {
	}
}

int my_func()
{
	while (1) {
		for (i = 0; i < x; i++) {
			frob_my_socks();
		}
	}
}

And I don't even need a comment to note that the inner loop frobs socks
because the name of the function tells me that!

-- 
"It's not Hollywood.  War is real, war is primarily not about defeat or
victory, it is about death.  I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk



More information about the OCLUG mailing list