Posted on September 16, 2006 by Doolwind

Pair Programming – Try It Now

For only the 5th time in my life as a programmer I took part in some pair programming this week.  I had forgotten just how much benefit there is from pair programming and decided to share some of my experiences.  Pair programming simply means two coders, sitting down at the computer together with one person typing and both people discussing everything they do.  Below is a list of reasons why I highly recommend everyone tries pair programming and adopts it as a part of their best practices.  While there isn’t a need to do it all the time I’d recommend trying it next time you are designing a large system or trying to solve a complex problem.

1. Building Mentor-Protégé relationships

For this to work, you generally need to have programmers of differing years of experience working together.  The more experienced of the two should be the one actually coding and they can discuss and talk through the decisions they make as they code.  Programming is generally a very solitary activity and I’ve found watching someone else with more experience to be invaluable as there’s only so much you can learn from a lecturer, text book or self-teaching.  Actually watching a master in the art of programming work their magic will give a lot of insight into where the protégé is going wrong and will help to build a relationship where they will feel more comfortable approaching the mentor to ask advice.  Simply sitting down and explaining to someone the good practices in programming can sometimes seem arrogant but watching someone code is far less confrontational.  There’s usually a lot of ego involved with coders and I’ve found pair programming a way for more experienced programmers to dispense their knowledge without stepping on someone’s ego.

2. Two heads are better than one

This benefit is more for the project than the individuals.  If there is a particularly complex or difficult problem that needs solving, it’s often good to have two people working together to solve it.  Rather than just discussing the problem, where ambiguities can creep in, having two people write the actual code means any misunderstandings can be cleared up as they write.  The code is the final.  Opinions, prejudices and communication problems just don’t exist within code; it’s the final contract for what the program will do.  Pair programming is also continuous and instantaneous, where sudden ideas that flash to mind can be discussed and implemented on the spot.

Some people may argue that it’s a waste of programming resources having two people do one person’s job, however I believe the benefit gained when working on complex or difficult problems far out ways the cost.  You really can’t put a dollar figure on having a better solution to a problem and while the time taken to write code won’t be half as much, the quality will often be high enough to warrant this higher expense.  The cost does mean that I wouldn’t recommend pair programming all day long as simple problems can often be solved by one person more easily.

3. Sharing specific knowledge

One of the biggest benefits from pair programming comes from the fact that every programmer is different and have different experience and knowledge in different areas.  Because of this, when two programmers sit down to create some code together one of the two will generally have more experience in the area and can share this experience with the other person.  This is similar to the mentor-protégé relationship however it’s at a lower level.  Despite the overall experience of either programmer, the person with the most knowledge of the particular domain should lead the way and share their knowledge with the other person.  If someone has written a number of implementations of the A* search algorithm then sitting down with another programmer and implementing a slightly different one for their current project will help that person share their specific knowledge.

4. In with good habits, out with bad habits

The act of having someone else watching your every move increases your awareness of bad habits you may have.  Better yet, if you aren’t aware of them, the other person can tactfully let you know of a better way of doing something, or that what you’re doing is just plain wrong.  On the other side you may find particular good habits that you can pick up from the other person.

So there are four simple reasons why I think pair programming is something you should at least try to see if it works in your given situation.  I’ve had only good experiences with it so far and I’d like to hear from anyone else either for or against the practice.