Last month, BDD founder Dan North visited Bergen to talk at their local NNUG event. I didn’t have a chance to attend (Bergen is a bit far; damn these Norwegian mountains :p), but I’ve talked to a few people who did and apparently it was a really good event. Jon Torresdal wrote on his blog that Dan North talked about this idea of giving the “I” prefix commonly found in C# interface names meaning:
“Dan said, why not use the I for something meaningful? Ask the interface the question: What do you do? And the interface says: ISendEmail or ISearchFiles or IPingComputers.”
I think this is a great idea. Doing this not only makes your code more expressive – it also helps you enforce the Single Responsibility Principle because it makes the concern of any given class very explicit:
public class WebFormsAuthenticator : IAuthenticateUsers
{
public bool Authenticate(string username, string password)
{
return Membership.ValidateUser(username, password);
}
}
And this not only works for any classes implementing the interfaces, but also those depending on them – it becomes especially visible if you’re using some form of dependency injection:
[Authorize(Roles="Administrator")]
public class UsersController : Controller
{
private readonly IManageUsers _users;
private readonly IAuthenticateUsers _authenticator;
public UsersController(IManageUsers users, IAuthenticateUsers authenticator)
{
_users = users;
_authenticator = authenticator;
}
/** snip **/
}
In the above example for instance, the fact that our UsersController seems to be concerned with both the managing and authentication of users is an in-your-face indication that it is possibly talking on too much responsibility (I say possibly, because it might very well be OK for it to depend on both).
I’m using this naming style in an ASP.NET MVC project I’m current working on to see how it holds up on a larger scale; so far it looks promising.