[Mono-list] Class Library coding standards

Thomas F. Burdick tfb@OCF.Berkeley.EDU
Wed, 18 Jul 2001 19:42:09 -0700


Nate Case writes:

 > Here is an excerpt from the Linux kernel coding style document (see
 > /usr/src/linux/Documentation/CodingStyle):
 > 
 > ---[snip]---
 > ---[snip]---
 >
 > I'd say this view is fairly widely adopted among the open source
 > community, but of course many people still disagree.  Nevertheless, it's
 > what we use for Gnome as well.  The only type of arguments I can sort of
 > understand against it in this situation is C# vs. C issues (if for some
 > reason it's impossible to keep C# code under 80 columns with 8-space
 > tabs)

Yes, this is *one* widely adopted style.  Another is GNU style, which
I find a lot easier to read for code that has lots of levels of
indentation.  The Linux kernel style tries to punish excessive
indentation.  GNU style doesn't.  Given that C# introduces gratuitious
indentation, I think the GNU style is a better match.

As for the clarity of where a control block starts, compare:
        int foo (int x)
        {
          int y, z;
          y = do_stuff (x);
          if (y)
            {
              printf ("do_stuff (%d) = %d\n", x, y);
              z = more_stuff (y);
            }
          else
            {
              y = try_stuff_again (x);
              if (y == 0)
                explode_and_die();
              else
                {
                  printf ("try_stuff_again (%d) = %d\n", x, y);
                  z = more_stuff (y);
                }
            }
          return z;
        }
with:
        int foo (int x)
        {
                int y, z;
                y = do_stuff (x);
                if (y) {
                        printf ("do_stuff (%d) = %d\n", x, y);
                        z = more_stuff (y);
                } else {
                        y = try_stuff_again (x);
                        if (y == 0)
                                explode_and_die();
                        else {
                                printf ("try_stuff_again (%d) = %d\n", x, y);
                                z = more_stuff (y);
                        }
                }
                return z;
        }

The GNU style sacrifices vertical space in order to make control
blocks clear without punishing nesting.  The Linux kernel style
sacrifices horizontal space to make control blocks clear without
punishing function length.  C# code has significant nesting built in;
I'd rather discourage long functions than ones with lots of nesting.