by edmundhuber · 8y ago · 0 comments
While it's true that not all opinion is fact (uh oh -- 5. sarcasm) there is nothing out of line in asking "Why didn’t you just do ___ here?" How you approach this question has a lot to do with your own perspective. If you are feeling defensive, it can seem like the code reviewer is calling you out. But if you have experienced people reviewing your code, this is a question that you will get a lot.
"Why didn’t you just do ___ here?" often simply means "I don't see why you couldn't have done it this simpler way, can you explain"? It's often for everyone's benefit that things are done in the simplest, most obvious way possible, and it's not productive to take offense at being asked to reconsider how you accomplished something.
It's a more complete statement and it avoids an extra back and forth for someone to say "What do you mean?", which is usually the response to "Why didn't you X?"
And it still accomplishes the same goal of opening a conversation, while better making clear you are seeking understanding.
It's also more useful to people who might refer back to the PR later, which happens surprisingly often in my experience. I try to treat PRs a little bit like historical documentation.
Suppose the code was "list.extend([1])" (Python). And the comment was "why didn't you just list.append(1)"? That would be a completely reasonable thing to say.
I think what I want to caution is that there is a middle ground that this article is ignoring. Yes, sometimes it is uncouth to just say "why didn't you just _". But it can also be important to have a little bit thicker skin, as sometimes you will get a comment that looks just like that, and there will be a good reason.
A better approach is to use the less loaded "have you considered doing X here?".
If you find yourself needing to demand others not take offense at your questions, you might wish to try a different approach.
A "Why[...]" question asks for why.
Assuming that "Why didn't you just do X?" isn't really just a disingenuous way to say, "you're an idiot; do it this way instead", then you should ask for what you want and ask the why question, not the yes-or-no question. In the case that it is a disingenuous question, then that's something that's covered adequately by #5. Suggesting the avoidance of sarcasm is already broadly applicable enough to cover using questions when you really mean to make a statement. (Which is not specific to code reviews—it's as obnoxious in real life.)
After that I usually reply with something like "Could you add a comment and/or test highlighting this reason? (So the next time someone modifies this code they don't make the same mistake I did.)"
Definitely better results all around.