August 30, 2009

Should We Be Prepending 'I' to Interface Names?

Prepending 'I' to interface names was a habit I adopted when learning C#. I guess this naming convention is a carry-over from COM days, but it's usage in C# is still in effect - in fact, it is a MSDN recommended practice: “Do prefix interface names with the letter I to indicate that the type is an interface”. Yet why it is a recommendation is confusing, as it conflicts with other recommended practices, like "Do not give class names a prefix (such as the letter C)".

Even though this naming convention was odd to me at first, I've been using it for a while now without much consideration. But once again, Robert Martin has me rethinking how I write my code. In Agile Principles, Patterns, and Practices in C#, Martin writes:

"In general, it is a bad idea to pollute the name of something with an orthogonal concept, especially if that orthogonal concept can change. What if, for example, we decide that ICommand should be an abstract class instead of an interface? Must we then find all the references to ICommand and change them to Command? Must we then also recompile and redeploy all of the affected assemblies?

This is the twenty-first century. We have intelligent IDEs that can tell us, with just a mouse-over, whether a class is an interface. It is time for the last vestiges of Hungarian notation to finally be put to rest."

I agree with this comment. Not using the 'I' would make my code more flexible to change. It would also lead to better domain names. And honestly, I really don't care whether the thing I'm working with is a class or interface; I'm more concerned about its behavior.

However, preceding interface names with an 'I' is a widely-used convention in the .Net community. It's one of the few standards that every developer I've worked with has used. While I don't condone doing something because "that's how it's always been done", you could argue that since using the 'I' is so commonplace, it helps with readability. But I think the best reason for including the 'I' is that it makes for easy naming of the default (and often only non-test) implementation of an interface.

Nevertheless I keep wondering why interfaces need to be marked as special when IDEs make them easy enough to identify. This and the fact that I ultimately don't care if I'm working with a class or interface leads me to believe that we shouldn't be including the 'I' in front of interface names. However, it's not a practice that I'm pushing our team to adopt, as the current convention is popular and works well enough. It is something, though, that I'll try out on my personal projects, and it's certainly fun to ponder and debate. For instance, check out Brad Abram's post on this and the comments that follow.

I'm curious to hear your thoughts. Should we prepend 'I's to our interface names? If so, what are the compelling reasons to do so?

2 comments:

  1. Don't forget that all the cool kids are doing this with that "I" these days: ISubmitBlogPosts.

    I guess that doesn't match the recommendation "DO name interfaces with adjective phrases, or occasionally with nouns or noun phrases." [Framework Design Guidelines by Cwalina and Abrams], but it's interesting how much easier it has made naming the few times I've tried it.

    ReplyDelete
  2. I've never seen that before. Intriguing. It would definitely make naming some interfaces easier, but I'm still not certain whether I should be adding an I to the front of interface names.

    ReplyDelete