doing what I used to do in the 90s at my first job. ! I’d get an email with a set of changes, print out all the code on a dot-matrix printer, write on it with a red pen, spill coffee all over it, then swear a lot. ! Eventually, I’d slop the half-digested pulpy mass onto someone else’s desk, then return to a similar pile in my own cubicle. ! Makes pull requests and comments feel pretty bougie, eh?
five years, depending on how we’re counting, I’ve done a few thousand code reviews. ! Every client has their own way of doing code review. ! Pair programming, pull requests, yelling random questions at a presenter… ! There are almost as many styles as there are teams.
motivations for doing code reviews fall into a few categories. ! *It can be a really great way to get new programmers familiar with the practices that a team has. ! *Among existing members of a team, new tips and tricks can be spread more quickly. ! *From a managerial perspective, it gives senior engineers a discrete arena to integrate styles. ! Code review can be a powerful tool for bringing a team together.
! In many languages, Ruby included, there’s a difference between single and double quotes. ! Some people prefer to use double quotes all the time for consistency. ! Other people only use double quotes when interpolation is needed. ! On one project I was on a couple years ago, the contents of the `config/initializers` directory changed on an almost-daily basis.
I’m going to walk through an example code review. ! There are two roles I’m going to talk about: ! *Reviewer *Author ! In this example: ! *I’m going to be the Reviewer. *we’ll talk about the Author a little later
a basic ActiveRecord model. ! *You shouldn’t use four spaces here ! *never use #map like this ! *you’re a junior programmer, and you probably don’t know this, but that’s not how we write Ruby ! *how can we make this function better?
be using four spaces here.” Let’s talk about what the Reviewer said first: ! *“You shouldn’t be using four spaces here” ! Of all the days I’ve been on client projects in the last year, ! maybe a handful of them passed without hearing something like this.
it’s not just about editor settings or git. It’s an appeal to an outside authority. ! Instead of talking about the merits of the code on the screen, the Reviewer is using objective facts or information from outside the current discussion. ! This feels like it’s coming from a totally rational place, but the problem is that code review isn’t really about teaching moments or trivia. ! There’s a solution to this problem that we as engineers can love.
of appealing to an outside authority during a code review, we can create one artificially to make these observations for us. ! That’s basically what Continuous Integration does with a test suite.
RIGHT For example: ! *Consider adopting or forking the Ruby Style Guide. ! *If syntax issues come up, add Linters to your continuous integration build. ! *CodeClimate for complexity, big methods or overuse of DSLs. ! *SimpleCov is great if tests aren’t being added for code. ! It’s easier to not get emotional if you’re arguing over a settings file.
language is used, like “always” or “never”. ! “Never use #map” is really confusing to the Author. I mean, we use #map for tons of stuff, and even this function might have a passing test. ! This is a tricky thing for engineers to address, because we love blanket statements. ! I mean, I wouldn’t challenge someone who says “Never use Sinatra” or “Always write tests.”
an ActiveRecord #select.” So instead of Generalizing, we should use specific language to address the problem code directly. ! *“I think this #map should be an ActiveRecord #select”
you wouldn’t understand that this isn’t idiomatic” “As a junior programmer, you wouldn’t understand that this isn’t idiomatic Ruby”. ! The things we’ve looked at so far are definitely about making someone feel like their practices are on the outside. ! This is much more direct.
! The Author will remember it for the rest of their career. ! There’s no way to unspeak those words, and the damage done can be permanent. ! While I was writing this talk, I talked to a lot of junior and senior engineers. ! One thing I noticed is that I did a lot of labeling, as did basically everyone else. It’s endemic to our industry.
returns are on their own line.” INSTEAD OF LABELING So what’s something the Reviewer can say instead? The Author may, in fact, be very new to Ruby. ! They might be new to programming in general. ! Well, just talk about the code. ! *“What I’ve seen in most Ruby functions is: returns are on their own line” ! There’s a couple things embedded in here.
functions is that returns are on their own line.” First the Reviewer uses language that focuses on themselves. ! Literally. Like use the words “I’ve seen.” ! This is one really easy thing to do that takes the focus away from the Author.
functions is that returns are on their own line.” Now that we’ve removed focus from the Author, the Reviewer needs to talk about the code directly. ! This can be really hard, especially if the Author wants to talk about their past experience. ! It takes a lot of practice to use these tools, but it’s worth it.
we make this better?” Alright: ! *“How can we make this better?” ! At first blush, that might not seem so bad! ! I used to work at a larger consulting company which prides itself on teaching client developers how to write better code. ! When I was running this by a friend who worked at the same consultancy, we came up with a clear motivation. ! The Reviewer wants to guide the Author using Socratic questioning.
The Author wrote this code, and the code review itself can imply that what’s there must be bad somehow. ! In deliberately hiding expectations, the Reviewer is creating a gulf that the Author needs to bridge. ! Pressure is put on the Author to come up with the correct answer is being combined with the Reviewer’s knowledge of inadequacy. ! The Author is being set up to fail.
appropriate here. Can we move it into the class?” The Reviewer needs to clearly state their needs to the Author. ! *“I think #define_singleton_method isn’t appropriate here. Can we move it into the class?” ! As a side-note, I’ve used Socratic questioning for years. ! I had a lot of great experiences using it as a TA, opening doors to learning. ! So it was jarring to find that this technique can be so destructive in another, related context.
clients! http://www.flickr.com/photos/80492365@N00/4960655570 In fact, I started to see my own actions through some kind of warped funhouse mirror of shame and regret. ! I know I’ve said these things before, myself. ! *What could it possibly mean? ! *Am I a bad person? ! *Have I destroyed people’s careers?
engineer who’s senior enough: to have memorized the Ruby syntax, to know what good code looks like, and to have opinions that could make them a great reviewer. ! Is an engineer senior enough: ! *to make others feel excluded from the team. ! Especially new engineers. ! Especially ones who need positive feedback the most.
being excluded. ! Everyone can be excluded, intentionally or otherwise. ! New team members feel excluded sooner because they aren’t fully included yet. ! That’s because the rapport that exists between members of an engineering team is something that builds over time.
you wouldn’t understand that this isn’t idiomatic” “I’ve been writing Ruby for 5 years” So the Reviewer says: ! *“As a junior programmer, you wouldn’t understand that this isn’t idiomatic” ! The Author might come back with: ! *“Well, actually, I’ve been writing Ruby for 5 years.”
here. ! They’re correcting a misperception. ! In reacting to negative criticism by defending themselves, the Author is inviting more of the same negativity. ! You can imagine how. ! If the Author has been writing Ruby for 5 years, what’s up with that terrible code on the screen?
that this isn’t idiomatic” “What changes should I make so that this code is more idiomatic?” As the Author, let’s give the Reviewer the benefit of the doubt. ! If they’re legitimately trying to express frustration, but failing to properly voice their needs, you can prompt them for that information. ! *“What changes should I make so that this code is more idiomatic?” ! Again, just like with the Reviewer, we’re steering the conversation back toward the code itself. ! Let’s look at another example as the Author.
#map this way.” “It’s a spike, so it’s okay for now” * “Never use #map this way” ! The Author might respond with: ! *“Well, we’re going to delete this code and it’s a spike, so it’s okay for now, right?”
another defensive posture. ! This has the same result as before: more negativity, even though there’s some truth to it. ! I mean, just because it’s a spike doesn’t mean you can kill the database.
should I write instead?” Again, let’s try to give the Reviewer some space to use the wrong words. ! The Reviewer has some idea of where to go with the code, but they’re being obscure. ! The Author could say: ! *“What should I write instead?” ! Again, this moves the focus back onto the code.
we make this better?” Remember that really weird question by the Reviewer: ! * “How can we make this better?” ! The Author might actually choose this moment to check their work email and respond to a thread about the highest score on Threes.
Instead of being open to feedback during the code review, it’s easier to just focus on something else. ! Recognizing that this is happening is really difficult. ! Disengagement can feel like something else has legitimately taken priority.
counter-critiquing is something I’ve seen happen between two senior developers. ! The Reviewer might say something like: ! * “I don’t like that this function is 10 lines long.” ! Then, the Author responds with: ! *“well, I saw you commit a 50 line function yesterday!”
is 10 lines long.” “How could I rewrite this function to be shorter?” Once again, the Author needs to give the Reviewer the ability to give feedback. ! In this case, the Reviewer’s needs are already clearly stated and reasonable. ! The Author could ask for more feedback: ! *“How could I rewrite this function to be shorter?” !
that the Author should state their needs. ! In the extremely formal social interaction of code review, the Author is looking for specific feedback about the code in front of them. ! The Author’s role in all this is very definitely about listening. ! Not just physically hearing, but listening to the Reviewer’s input.
than evaluating code. ! *The Author of the code under review is attempting to build rapport with the Reviewer. ! *The end goal is that the Author wants to be included in the engineering team at the same level as the Reviewer.
see if the Author fits into their group. ! Code review is just the way we most commonly do this on an engineering team. ! Group identity can be established in a rather large number of ways. If looking at code was the only way we did that, great teams might spring up overnight. ! But it’s not.
Graham gave this quote in an interview last year. ! It’s pretty clear what he thinks of accents, but what is he saying afterwards? ! He’s not sure why. ! He just knows they don’t fit into his group. ! Does Paul Graham know what his group selection criteria is?
despite what Paul Graham thinks. ! It happens to everyone, because jerks are everywhere. ! So how do you know when it’s happening to you? ! Well, it turns out there’s something so common that it’s an everyday phrase.
stress is physical discomfort. ! I usually notice myself fidgeting. I usually don’t do it unless something’s up. ! Your body might have its own expression. ! For example, one of my friends notices that her shoulders tense up and she gets really bad back pain.
If you and your coworker are just staring at the code, avoiding eye contact, not talking, then something is definitely up. ! What I notice is that I’m in this silent moment, and I’m not really sure how long I’ve been there.
my first job where I was doing code review on a full-time basis, I would fall asleep even before lunch. ! The constant drumbeat of physical stress wore me down.
stress is by encouraging at-work drinking. ! Drinking won’t make stress go away, but it may open up a bunch of other dangerous situations. ! I won’t go into those tonight. ! As a side note, if you’ve modeled your office after the inside of Lefty’s on Saint Patrick’s Day, you might have a culture problem.
come back from a break. ! If you’re the Author and the Reviewer isn’t backing off, it’s time to call it a day. ! There’s one thing you can say here: “Can we pick this up tomorrow?” ! After that, you can go home or work on something else, but the code review is definitely over.
Game I’ve given you a bunch of tools for recognizing when a Reviewer is using exclusionary language. ! *Being right, an appeal to an external authority ! *Generalizing, the use of absolutes ! *Labeling, the act of excluding the Author directly ! *Playing the guessing game, getting the Author to exclude themselves
out •Counter-critiquing I’ve also talked about ways the Author can catch themselves defending against exclusion. ! *Correcting misperceptions, a way of engendering missing respect ! *Justifying, a plea for sympathy or mitigating circumstances ! *Tuning out, allowing exclusion by the Reviewer ! *Counter-critiquing, preemptively excluding oneself from the Reviewer’s group
There are also signs that you need to take a break: ! *Physical discomfort ! *Silence ! *Exhaustion ! There are other signs of physical stress than what’s up here, and you know your body’s language better than anyone else. ! These are just what I notice.
•Avoid “you” Finally, there are some ways for the Author and Reviewer to be more inclusive: ! *Talk about the code on the screen ! *Use self-directed commentary, sentences that begin with “I feel” and “I think” ! *Avoid talking about the other person, specifically be aware of the word “you”
aware of behaviors and practicing, but you probably won’t notice changes right away. ! It’s just like with meditation or yoga. ! When you’re better at it, you’ll know because it’s easier than it was six months ago.