Convention over Configuration
Just a quick note on Convention over Configuration, I believe this is one of the more useful practices that you can apply in your codebase. So what is this all about then? Well think of about everything you do in life; think for example about “opening a door” or “turning on the water” we all exactly know how to do those things, and because of that it is a fast action. It is something that we don’t have to figure out just before doing it, again and again. These are conventions.
When we start applying these sorts of ideas to our code base this would mean that certain operations need far less thought as they will be the same each and every time. A good example here is mapping the controllers and view together and extracting the URL from the controller name in MVC. If you are a developer that knows these conventions then you know exactly where to look for the code of a certain URL. No need to figure this out each and every time.
Conventions also bring an opportunity to automate things within your system, because you are following certain rules in your code, you can write some other code that does something accordingly to these same rules. Configuration is a area that greatly benefits from conventions. Instead of having to manually configure how each different part works together you can write some code that configures this for you. You can also write some tests that verify your conventions.
Configure your Conventions
Many frameworks apply conventional thinking in their code, making it easier for their users to use it. There are frameworks like Ruby on Rails that rely very much on conventions, the only thing here is that these conventions cannot be changed very easily. This makes the framework very opinionated. This is not necessarily a bad thing in fact this means that any Ruby on Rails developer can jump to any other Ruby on Rails project and start working on it (sort of).
But frameworks that allow you to configure your own conventions are much more powerful, because not all conventions make sense in every scenario. FubuMVC is one such framework where we try not to be to opinionated (we are) so that you may configure your own conventions.
Not just for framework code
I try to figure out what conventions that are in a system that I am working on and make them explicit as soon as it makes sense. The benefits are clearly there in my eyes, being able to test them and apply them is great. This is not something you would only want to apply in framework code, for example the win forms application that I created in my CQRS example also uses conventions to hook up the views with the presenter. Very easy and I don’t have to configure it manually ever again.
Now adding a new event to the application is like turning on the water or opening the door.






What you are talking about are not conventions, they are patterns.
Any developer will be able to understand another developer’s work as long as he can figure out the patterns the code follows.
I’m sure you can view those patterns as the conventions the developer expressed, but I’m not sure it’s the best way to express it.
In my opinion a convention is the way the framework was tailored so that it fulfils a certain operation like matching the url to an action on a controller. This generates a pattern in which the user of the framework constructs inside the controllers directory a new class with a certain name and defines a certain action inside that class.
Opening the door or turning on the water are just patterns you know, the convention would be expressed like turning the door knob for opening the door, or rotating the valve to the left to get the water.
There is a subtle difference between the two that I didn’t saw expressed in your article. Anyway it depends on the way you taught yourself to think of this things.
@Mihai
Interesting take on it, I would say that a convention is that you apply the same pattern to each case. You can use patterns without introducing a real convention. Perhaps I would say a pattern is the result applying the convention?
Thanks for your feedback will think some more about it.
@ Mihai
(a.k.a the “Heeere’s Jhonny!” anti-pattern.)
I think that example might be backwards. Turning a knob to me would reflect the pattern. Other patterns might be knock out the pins, or use the fireman’s axe.
Conventions would be the implicit application of patterns. The whole convention over configuration argument was specifically raised to adopt a convention to avoid unnecessary variability. “Unnecessary” might not be the best word for it, but reflects that convention aids readability by it’s consistent nature where configuration can vary at any time in a multitude of ways. Convention has been around forever, but now the lines are blurring with the concept of “pattern”. Hungarian was a convention for naming variables to reveal their type. The difference with something like MVC or Rails is that the convention was not enforced or acted upon by the platform.
Nice article dude, hate to nitpick but in the line:
“This is not necessarily a bad thing infect this means that any Ruby on Rails developer can jump to any other Ruby on Rails project and start working on it (sort of).”
‘infect’ should probably be “, in fact, ”
Keep up the great work!
@Mark Rogers
Thanks, I need nitpickers especially to get my spelling mistakes. Fixed!
I really like the idea of Convention over Configuration, but I also have reluctance on its use everywhere.
Convention seems ok when you replace a configuration in one place by a convention in another place.
But it seems to lead to brittle code when convention is used to link two parts together, because only convention specifies that when one side change, the other side should change too.
As long as you cannot configure your refactoring tool (R# or any other) to check these conventional links and enforce it, you’ll break your code any time you make a typo, change a name etc.
Of course you can rely on your unit tests, but I don’t see the benefit of convention if you must add unit tests only to check that conventions won’t break…
@Think Before Coding
It might be some of my compulsive behavior (seriously I need certain stuff to be true) but my code will be very conventional anyway. So I am using it to make my life easier.
Also the reliance on tests would be there anyway, but instead of having one test for each different use-case I can write one tests that will verify all use-cases. Also my tests are somewhat smart in that they will get all exceptions and tell me exactly what is wrong. So easy fix. And I have also certain stuff just in the code that throws is a conventions is not met.
So yes I try to push it, and so far it hasn’t given me any pain at all