Back in 1997 I remember my first day at high school. It was a memorable experience because the first day of high school is obviously a big deal in the life of an 11 year old, but also for another reason. The building was split into two almost identical sections, each with a giant staircase which took you up through the different floors. I wasn’t actually aware of the two separate sections of building to begin with, causing a surreal feeling when I noticed that the stairs were changing each time I walked up them. If the pictures on the walls had started talking to me, I may have accepted it.
If you don’t have a mental image already, I was a 5ft tall, glass half full, 11 year old preoccupied with the single staircase that was apparently tricking me every time I used it. This unhealthy relationship with the staircase resulted in me thinking much more about the staircase than the average staircase-climber. The same day, having realised there were actually two very similar staircases in the building, I also noticed something else that had been eluding my narrow focused attention.
Everyone was walking up the stairs on the same side. Literally half of the width of the staircase was being wasted – a completely unused freaking gap on the staircase. Why is everyone acting like a robot? I wondered. Why are they wasting all this space? Being the bottom of the pile, I followed suit and got in behind the seemingly stupid queue of people standing beside a meter of empty space.
As I climbed toward the third floor, I saw a sign saying “Keep Left”. Things were suddenly starting to make more sense. Curiously looking around, I noticed another sign located opposite from the sign I’d just seen, “Keep Left” again. My aha moment came with a slight sense of embarrassment as I remembered labelling the people in front of me as morons for wasting staircase space. Everyone sticking to the left when going either up or down meant that no one got in anyone else's way, making the rush between lessons notably safer. Thanks to a simple convention, the use of the staircase became significantly easier and more effective.
Convention is all around us, proving on a daily basis that it makes our lives easier. When you pick up a magazine, where do you expect to find a list of what’s in it? Through convention, you expect to find the table of contents on the page following the cover. How about when you’re in a bookstore and you notice your favourite book “The Life of Flowers”. You paid a reasonable £79.99 for it on the day it was released, but you want to know how much you would have paid had you been able to control yourself. You pick up the 1500 page tear-jerker and look for the price by turning it over and checking the lower half of the back cover. You know the price will be there because convention tells you that’s where the prices are placed on books.
Making things easier is the holy grail of software development. When things are difficult, not only do they take longer (and consequently cost more to a business) but they have an increased risk of failure. It’s surprising how often I come across code where the programmer who wrote it cannot make basic assertions about the purpose of certain variables, or what certain methods of the class are for. Their code is a convention-free nightmare, where each and every reasoning about the code has to be carefully calculated because nothing can be derived from any entity of code individually.
Convention is critical if you’re planning on writing code that you can easily return to. Sure, you can write comments – but comments don’t necessarily make your code any easier to read. That integer named bob still has absolutely no semantics by itself, and comments in these situations aren’t the answer – naming variables so they don’t need comments is a much better approach. Naming is an aspect of programming where the use of a convention is more common. For example, back when Win16/Win32 programming was prospering, programmers would use conventions such as Hungarian Notation to indicate that a variable was a long pointer to a string terminated with a zero: lpszVariable. Though lots of people religiously disagree with the use of Hungarian Notation, it’s an undeniable fact that it adds some meaning. From the convention, we can assume the variable is a far pointer to an area of memory that holds textual data and ends when a zero is encountered.
Too few people go beyond a simple naming convention, yet most people understand the benefits to be had from the use of a well designed convention. Creating a convention for some aspect of your project is like creating your own operational pattern. When the pattern is followed, it makes that aspect of the project easier. On the other hand, no convention means no pattern and half your time spent working out where you put that class that handles the data access. Oh, and what did you call it? And what the hell does the class called bob do?
So I leave you with this advice: when you start your next project or write your next class, take a moment to think about conventions you might want to put in place. In environments where a team of people will work on the project, a convention can have a mighty effect on how people understand the organisation of the project and avoid the inevitable mash-up of everyone’s own convention-free code. Apply conventions to things like the physical file/folder organisation of the project, so that adding new files to the project becomes easy and meaningful. Use design patterns such as factory method consistently so that you have an established convention for instantiating objects and everyone understands where to instantiate objects from. Should each source file contain just one class, or can it contain many? Should the class name match the file name, making the class easier to find from looking at the source tree? Aside from code, do you have a convention that dictates how code is tested and ends up in the production system? Consider answering these questions by defining a convention.
10 points for those who noticed the Steve Ballmer reference.