[Mono-list] Class Library coding standards

Patrik Torstensson Patrik.Torstensson@framfablabs.com
Thu, 19 Jul 2001 10:02:11 +0200


But look at this instead, I think it's easier to read:

        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;
        }

I think a if statement always should have {} because it minimize the
risk of errors.

Just my 2c again.. 

- Patrik

-----Original Message-----
From: Thomas F. Burdick [mailto:tfb@OCF.Berkeley.EDU] 
Sent: den 19 juli 2001 04:42
To: Nate Case
Cc: Tomas Restrepo; mono-list@ximian.com
Subject: Re: [Mono-list] Class Library coding standards


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.

_______________________________________________
Mono-list maillist  -  Mono-list@ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list