What is Pair Programming?
Pair programming is a practice in which two developers will team up to write some code by sharing a computer. There are two common types of pair programming: "diver/navigator" and "ping-pong".
In driver/navigator pairing, the "driver" is the one doing the typing while the "navigator" provides higher-level direction. The developers will swap roles every so often, at intervals no longer than 30 minutes.
In ping-pong pairing, one developer will write a test and the other will write the code to get the test passing.
Both techniques require good communication skills and close collaboration. It's not always easy or intuitive to pair. It can be a difficult skill to develop. Before we learn more about how to pair successfully, let's understand what's so great about it in the first place.
Why Should You Pair?
There are a number of things you win when you pair with a teammember to solve a problem or build a new feature.
- Increased code quality
- Knowledge sharing within the team
- Juniors level-up faster
- Happier developers
Increased Code Quality
When two people are designing and implementing code together, that code will come out cleaner and less likely to contain bugs. In order to successfully pair, you have to first plan out the code you're going to write. You and your pair need to think about and discuss the direction of the code together and you're more likely to uncover potential bugs and edge cases when you discuss it together, than when one person works on it alone.
Additionally, having two sets of eyes on the code as its being written will help you catch bugs and other errors before they become a problem.
One problem that a lot of engineering teams face is the siloing of knowledge. What happens when one person builds a complicated feature by themselves from start to finish? That developer is the only person on your team who knows enough about the feature to refactor it, build new features on top of it or debug critical issues that arise. This is a big problem for a number of reasons.
That one person is likely to burn out after being burdened with the development, deployment, debugging and future phases of one project. Other team members are robbed of the opportunity to learn from or contribute to the feature. And, worst case scenario, that one person quits or moves to a different team. Now how are you going to continue to dev on that feature?
Pairing prevents all of this. When two people work together on a feature no one developer burns out and the two pair members can more easily disperse knowledge of the new code to still more people on your team.
Juniors Level-Up Faster
Pairing is one of the best ways to train junior team members. By pairing on bug fixes and new features, more experienced team members can introduce juniors to the existing code base, share their own expertise when it comes to technical challenges and expose juniors to other important skills like how to use Git.
When team members pair, they communicate. A team that communicates well and its accustomed to solving problems together is a cohesive and healthy team.
When team members share the responsiblity for developing features, they are less likely to be stressed. After all, the burden for delivering a successful feature no longer falls on just your one set of shoulders.
How to Pair the Right Way
Now that you're convinced of all of the benefits of pairing, let's talk about how to pair successfully. There are few key things to keep in mind:
- Plan out your code together
- Code out loud
- Take turns
- Take breaks
- Listen to your pair
- Respect your pair and their space
Plan Your Code Together
Before you start writing code, think through the problem you are trying to solve with your partner. Discuss different designs and approaches and decide on a solution together. This gives both pairs the opportunity to ask questions and raise issues. It ensures that both partners feels good about the direction they are taking the code in and understand what needs to be done.
Code Out Loud
If you're the driver, you should practice "coding out loud". That means you will actually state what you are trying to achieve with the code you are writing, even just by reading your code out as you type it. This will let your navigator know what you're doing and what direction you're going in. It will help keep your navigator engaged while they're not typing.
Its important to actually switch roles when you're practicing pair programming. It can be really easy to get into the flow of the code you're writing and let several hours go by without changing roles. This is a mistake! Set a timer and stick to it.
While pairing has a lot of benefits, it can also be really tiring. You're thinking out loud and doing the hard work of communicating with another person for hours at a time. Try taking a 5 minute break every hour and a longer break every few hours, at least. Give you and your partner some time to re-group so that your body and mind are ready to face whatever challenge your project throws your way.
Communicating with your pair is hands down the hardest part of pair programming. You might not always agree on the direction to take the code or the right solution to a tricky problem. When you experience a disagreement, give your partner ample time to describe their thinking and their proposal. Ask lots of questions and allow yourself to consider their ideas fully.
Beyond just listening to your pair, work to respect their ideas and their personal space. Sharing a computer can be challenging. Make sure you set up at a desk with plenty of space for both partners and be mindful of the space you're taking up, the snacks your eating and the overall needs of your pairing partner.
Practice Makes Perfect
Pairing is a tough skill to develop, so don't hesitate to just try it out, using the tips provided here. Happy pairing!