Roy Osherove

View Original

Create More Readable Code By Interface Prefixes

Here's something that might help write more readable code:

When defining a class that implements an interface, instead of just declaring the interface overrides like so:

      public class MyCustomHandler:IConfigurationSectionHandler

      {          

            public object Create(object parent, object configContext, System.Xml.XmlNode section)

            {return null;}

      }

you can explicitly prefix the name of the method with the name of the interface and a dot, and removing "public" modifiers from the signature, thus making it look like this:

      public class MyCustomHandler:IConfigurationSectionHandler

      {          

            object IConfigurationSectionHandler.Create(object parent, object configContext, System.Xml.XmlNode section)

            {

                  return null;

            }

      }

This makes for a more readable and explicit code, and states exactly which methods belong to what interface. It also saves writing comments for when their really needed. Could be a great way to show coding examples that do not use #region fields (not to mention that some coders don't use VS.NET, and their IDE does not recognize #region fields - this makes the code *tons* more readable)

another thing about this:

If I write object IConfigurationSectionHandler. in this class I'll automatically get a drop-down of the methods of IConfigurationSectionHandler. This works in VS.Net 2002(!). unfortunatly, it does not stuf out the methods parameters as well, but it's a nice tidbit to know...