Code Reviews: Before You Even Run The Code
Coding isn't all about writing and running. Like any writing, it's also necessary to read and edit. Perform a code review before you even run your code.
Join the DZone community and get the full member experience.Join For Free
i do a lot of code reviewing, both in my day job as principal developer and also as an open source maintainer. sometimes it seems like i read more code than i write! is that a problem? i'm tempted to say that it isn't. to be a good writer, you must be well-read; i believe that to be a good developer, you need to be code-omnivorous and read as much of other people's code as possible. code reviews are like little chapters of someone else's code to dip into.
over time i've developed some particular processes that i find helpful when reviewing code. in particular, i often surprise people at how much review i do before i run the code. sometimes i grab the branch so that i can use my local diff tools, but i don't actually execute code until i've established some basic facts. this post is a little insight into what's happening in this not-running-the-code-yet zone.
do you know what this change does?
this problem comes up more in open source than it does at work (but not exclusively), i get what looks like an excellently-formatted patch from someone .... and i have no idea why i would want to incorporate this feature or support this code for eternity. if i have no idea what feature you are adding, or what problem this solves, your pull request will get closed at this point. the trick here is to spell out to me why this is awesome and i should immediately get this code into the project.
does it relate to a ticket (and only one ticket)?
i assume your project uses some way of keeping track of tasks, issues, bugs, however you name them. find out if there is already one of those things for the change you want to make, and reference it in the pull request. i wouldn't reject a change without a ticket number (some teams will), or simply because it covered more than one ticket, but it's more likely. a change covering more than one ticket that really aren't closely related will probably get closed at this point. a couple of related tickets, or a great idea that didn't have a ticket .... those i can entirely engage with.
do these changes touch the tree as you'd expect?
if you think you're getting a new database column and there's a change to the css file, that should be obvious just from looking at the list of changed files. i like `git log --stat` for this but the web interfaces such as github will offer a summary view of a diff (click on the "n changed files" link) as well which is the same thing. look out also for surprisingly high numbers of changed lines - last week i spotted that we had accidentally committed a minified version of something for example, and you don't need to get as far as the diff before that sort of thing is obvious.
if you see unexpected changes, or changes not where you would expect them (this second one requires a lot more experience), then ask questions. i like to be patient but especially at work, if i see a mispackaged change which includes too much stuff or is clearly missing a file, i just reassign it back to the developer to get them to update it.
(the changes you expect should also include database patch scripts, test, documentation, etc. those are the files that can be hard to spot if they're not there!)
does the commit history make sense?
as a person who has been known to tell colleagues "that is not a commit message. try again", i'm quite fussy about this. the reason is purely that i know how much i have hated my past self for rubbish commit messages in the past!! a good commit message follows on from a good commit, so if you are having trouble writing a short and coherent message, then consider stopping, resetting your staged changes, and adding in smaller units to make a larger number of self-contained commits. there's a great article on writing commit messages that i think can really help: http://chris.beams.io/posts/git-commit/ .
other commit message weirdness to look out for here includes seeing commits that don't belong in this branch. whether you choose to unpick and recreate the change or not is up to you but pull requests that include a lot of other commits from another change (which might have been rejected) because the contributor branched from the wrong place have eaten many hours of my spare time! i usually think it is worth trying to correctly package all changes; as a developer you can check that it's going to look ok by scrolling down the page when you create the pull request - it will show you the commits that will be included and the diff, so if you're branching or requesting a merge to the wrong places, usually you can tell this is the case!
eyeball the diff
before i really get into detail with the code, i do cast an eye over the diff and look for anything really strange. these would include:
- large sections of commented-out code
- copy/pasted code
- silly variable or function naming
- any syntax so exotic that i have to ask what it does .... always ask! either you're picking up an important potential issue, or you're about to learn something. both are great outcomes :)
whether i will close a pull request with these mistakes in really depends on the context. if i have to work with this person again, then i will comment and request the changes i want to see to make it right. if it's an open source contribution and i think it's unlikely to be amended or if i urgently want to ship the change, then i will sometimes fix some things myself. it's very common to see a merge request come in from me that it someone else's branch plus one more commit from me fixing something tiny ... totally worth getting the spelling right in the comments, or maybe the route name the developer chose didn't fit our overall scheme - if i do this, i;ll mention it in the discussion on the pull request. if it's small, i always consider this option - but since i don't want to be cleaning up after the same people making the same mistakes for the rest of my life, i make actual colleagues fix things themslves!
now run the code
it might seem strange to have done so much with a pull request without even seeing if it works, but i think all of the above are important for the health of the project. if a change isn't correctly packaged, can't be run on other platforms easily, or decreases the quality of the existing codebase, then you don't want it. by making these inspections up front (especially that first one: what is this actually *for*?) will very often save a lot of time later on, but as developers we love code and the temptation is to jump straight in. i expect all members of a team to take on code review, and for them to take it seriously. when i do see a bug in a newly-merged branch, i ask first who reviewed it, not who built it. hopefully some of the ideas i've shared here from my own practice will help you in your work, too! if you have other tips to share, i'd love to hear about them in a comment.
Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.