Adapting to a Coding Style

A lot has been said and written about coding style. I’m trying to be a good citizen and comply.

To increase my compatibility and reduce friction (not overcome utter boredom!), I’m adapting my coding style to some general guidelines, e.g. given by Larry Osterman.

I’m used to BSD with a lot of spaces around parentheses, so I have a hard time setting the spaces differently. Actually, after three days I still have to correct most of the stuff afterwards.

So I’m asking myself whether there are subtle advantages to my spacing style.

Here’s an example in the style I’m trying to adapt:

private class RegexCaptureTreeItemGroupNameComparer : IComparer

{

      #region IComparer Members

 

      public int Compare(object x, object y)

      {

            RegexCaptureTreeItem itemX = x as RegexCaptureTreeItem;

            RegexCaptureTreeItem itemY = y as RegexCaptureTreeItem;

 

            if ((null == x) || (null == y))

                  return 0;

            else

return Comparer.Default.Compare(itemX.GroupName,
    itemY.GroupName);

      }

 

      #endregion

}

The region and the method stub have been added by Visual Studio, already in “good” style.

My original take was

            if( (null == x) || (null == y) )

                  return 0;

            else

                  return Comparer.Default.Compare( itemX.GroupName,

                        itemY.GroupName );

I know, “if” is not a function, but with the “correct” style I’m likely to omit the enclosing parentheses. Leaving the space after, resp. before the parentheses in a function call makes me omit the space after the comma as well.

Well, I guess I’ll give it some more days of trying before I’ll come to a conclusion on these issues.

Another thing is private variable naming. Ideally, the style easily differentiates between static and instance fields, parameters and local variables.

The standard approach for member variables is

private static MemberType s_StaticMember;

private MemberType m_InstanceMember;

I think, the prefix can be ommited and static members can be decorated with two underscores.

However, for parameters and local variables, I’m unaware of any good practice. You don’t want to prefix parameter names because there might be publicly visible and p_index doesn’t look like a practical local variable either.

In my original style, I didn’t make any difference between those four.

But that’s not helping anybody else to understand your code, even more yourself in three month’s time.

Advertisements
This entry was posted in Best practices. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s