Coding Alone In The Dark

“Code Smell” is an expression given when glancing at some code and seeing right away potential issues with the way it is designed. Martin Fowler describes it as “a surface indication that usually corresponds to a deeper problem in the system”. It is often tied with the notions of pasted code, anti patterns, over complexity, etc. I personally like the image of the expression: One barely needs to look at the code to know that there might be something wrong with it. Today, there are hundreds of articles that talk about how to detect code smells. There are tools that spit out metrics letting you know what could be improved. You get the idea…

Today I want to talk about another type of code smell : code that has been entirely designed and produced by a single person, code which has never been seen or reviewed by others peers, code that has been produced in the dark is almost always a code smell. (By definition, this excludes anything that is open source). This doesn’t mean that it is bad code, but most likely that it can be improved and perfected if viewed by and discussed with others. Even though there may be some exception to the rule, I have personnally never seen this theory fail once in all my years as a software developer. If you are doing it alone, you are doing it wrong. 

When you are aware that your code will not be seen by others, it takes an extreme amount of self discipline to not take shortcuts. Documentation will be sparse, routines will not be optimized, refactoring might not be complete. You might produce code that works, but if you honestly ask yourself the question: “is there anything I would change if my code was to be open sourced to thousands of developers tomorrow?”, chances are you will come up with a bunch of TODOs.

On the other end, coding “in the light” of others only brings positive things. You will be more careful when writing for others than for yourself. You will worry more about small details, such as the way you name your variables. You will make sure that your logic and algorithm are implemented and tested correctly. Your code will be easily readable by anyone. And once your code is out in the open for others to analyze, it will only improve with time. The feedback from others will make you a more humble and a greater coder. This is, I believe, the true gold that is provided by open source development.

At most private companies, designing the code for a feature usually involves multiple people. Code reviews are done prior to features being tested. Everyone knows the ROI of taking that extra step. Yet every so often, because of time concerns, of pride, or even worse because of fear of appearing dumb, code falls through the cracks, burns bridges and ends up being shipped in your software without having been looked at.

While I’ve had many occasions to avoid putting my code in the light, I found that I never had a good, solid reason to do so. Lack of time? Fear of what other devs might say? Lazyness? Pride? I’ve personnally gone through all these feelings many times. Asking for and accepting feedback is not easy. It hurts to realize that what you found great, others found perfectible. It sucks to realize that you have to spend more time refining code you thought complete. Noone likes to be told that he is wrong. Especially not coders, where personal pride and self justification too often come in the way of building greater code.

You’re probably thinking: “well yeah, you’re just stating the obvious: code reviews improve code quality… yadi yada.” Yes, it is obvious. But it’s not a natural thing for developers to do! We coders are mostly cavemen. We like the comfort of privacy, we need to experiment and create on our own. It is not a natural thing to bring our code in the light. I have heard and seen so many times developers who were left alone to create and publish code, in the name of trust and autonomy, only to realize too late that what they had created was awkward, unstable and prone to WTF moments. Just a few weeks ago, I stumbled upon a database schema that had been created by a mercenary who had only stayed a few months at our company. It contained a bunch of 3-letter tables with each 20-30 columns that had 3-letter names… No-one had a clue of which column stored what. The developer had been left alone for 6 months because of his seniority status. He only left behind mysterious, unoptimized and non logical code. Was it his fault for not showing his code to others? The peers’ fault for not asking? When a team cannot work together to produce high-quality code, it’s more a team issue than an individual fault. Coders shouldn’t feel forced to share their code. They should do it out of desire, because they realize the outstanding positive impact that it can have on his/her own coding skills and on the code itself. 

Don’t code alone in the dark. Talk about your code, write about your code. Share it, ask your peers to look at it, to discuss it, to criticize it. Get everyone involved in the process. A junior developer will build confidence when trusted with the opportunity to criticize code produced by his older peers. He will learn from analyzing good code. A senior i’ve-seen-it-all developer has the ability to look at the problem with a renewed eye and can be a trumendous asset. When given the opportunity, take the time to review your peer’s code. Learn from it. Find what could be done better. Outline the great things that you see, discuss the issues that you find and give constructive feedbacks. 

Beautiful code never comes out of the dark, it is always refined through the fire of analysis and criticism.

[edit] : I received a lot of comments on reddit / codeproject about things which weren’t understood the way I intended them to be. I am not meaning to say that code produced by a single person is bad code or “smells”. I am using the terms “doing it wrong” because there is so much more to gain from working on code as a group of individuals than as a solo dev. I have modified the article to reflect that.

Crucible: unrecognised diff format. expected diff hunk descriptor but found

At qualys we use crucible. Great tool though it can sometimes be a PITA… Like today for example.
I had a pretty big patch made from several svn revs:

svn diff -r 23284:23747 -x -b > ~/tmp/code.patch

When I tried to upload that to crucible, I received the msg: "Error adding patch: Unrecognised diff format. Expected diff hunk descriptor but found:"

It's a known issue from atlassian that has been alive for more than 2 yrs: https://jira.atlassian.com/browse/CRUC-6114.

Thanks for fixing your bugs so fast btw...

Anyways the pb comes from the fact that the diff command gathers metadata as well as code changes.

Turns out there is a great tool called filterdiff, part of the patchutils package that cleans that up well.

brew install patchutils
filterdiff --clean code.patch > clean.patch

Crucible should stop whining after that