1. Dec 1st, 2006

    Proof reading is more important than composition

    If you’re writing a piece of code that you never intend to read, following coding conventions is a waste of time. Computers, I can guarantee you, don’t care how you cross your t’s and dot your i’s.

    In fact, everytime you’re following a coding convention, you’re probably doing more harm than good.

    I spend 90% of my time writing code. I spend the other 90% reading the code because I need to add a feature, troubleshoot a problem, or path a bug. I actually read more lines than I write, my estimate is around 5:1.

    How productive you are depends on how quickly you can read those lines of code and get what they do, so you can find problems and make changes.

    Now, it sounds counter intuitive when I’m telling you not to bother with coding conventions, but that readability of your code is so important. But it all makes sense when you reflect on how we actually write code.

    Coding conventions focus on what you write, and following coding conventions quickly turns into a self-serving activity that guarantees your code is readable …. but only the first time you write it.

    What happens often, as you’re adding new features, change code and applying fixes, you make a mess. The code was coherent because you thought it will only be written one way, but now it changed so much that it’s no longer following the original narrative.

    And if you leave messy code like that, it only becomes worse.

    So here’s a different recommendation. Forget about coding convention, just write the code. But when you read the code, follow a simple practice. Fix where you hesitate.

    Every time you pause for a few seconds trying to understand a piece of code, you’re dealing with unreadable mess that will only get worse. So fix it, right there and then. Chances are you’ll have to go over it again, so now’s the time to smooth it.

    What this leads to is something much more effective than coding conventions. It leads to maintainable code.

    First, you learn what readable code looks like, so you know how to write readable code to begin with. And coding conventions don’t even capture the tip of the ice berg. They don’t deal with too many state variables, indecipherable algorithms, or spaghetti flows.

    By fixing what’s hard to read, you practice the art of making yourself understood.

    Second, as you grow your code, you’re starting to create a mess. This way, you can keep it clean and tidy with routine maintenance. You’ll quickly learn where to fix patchworks so they don’t grow into chaos.

    I’m reminded of that after looking at three autrocities I managed to create in only a few hours of coding.

    Through some merging of code, I ended up with two functions. One called authenticate and one called authentication. I choose two descriptive names which made sense in the two separate projects from which I copied them. And they’re both clean, flow and well documented.

    But for the life of me, I can’t remember which to call when, and ended calling the wrong function in the wrong place.

    Through the miracle of rapid iteration, I ended up with a function that works two different ways, depending on the value of its first argument. If you look at the function, it’s clear what it does and why it works that way.

    But how come I have a piece of code that calls the same function twice, expecting different results, and what exactly are those two different results?

    And the last example is just inconsistency between two different models that provide the same function with two different names. And I can’t remember where to use which one.

    So instead of worrying about how I write code — there’s just no way to prevent the mess — I worry about how I read code. What I don’t read, I don’t touch. But when I hesitate, I fix.

    Photo by headcase

    1. Dec 1st, 2006

      Paul Brown

      Lazy evaluation applied to formatting. I like it.

    2. Dec 2nd, 2006

      tnkgrl

      You mean: “I spend 10% of my time writing code. I spend the other 90% reading the code because I need to add a feature, troubleshoot a problem, or patch a bug.”

      Right?

      Great article, BTW! More material to stir shit at work woth…

    3. Dec 2nd, 2006

      Assaf

      It’s a pun on the ninety-ninety rule.

      In practice it actually takes 180% of the time, and I only get 90% of the functionality, because at 110% features start falling off the project plan.

    4. Dec 2nd, 2006

      beza1e1

      You approach “lazy writing for readability” doesn’t conflict with coding guidelines. Why shun the guidelines?

    5. Dec 2nd, 2006

      Assaf

      I think coding conventions conflict with it, not the other way around. It gives people the wrong thing to focus and a plan that assumes it will be written right to begin with.

      But if you have people working on the same code base, and fixing everything that doesn’t look right, eventually you converge on a common convention.

    6. Dec 2nd, 2006

      Chipping the web – 00001111 — Chip’s Quips

      [...] Assaf offers a maxim for keeping code maintainable: When re-reading, fix where you hesitate. [...]

    7. Dec 3rd, 2006

      Crosbie Fitch

      Even for solo coders, a coding convention can be very helpful.

      I often think lazy handwriting is fine given only I read it, but then n months down the line I can’t figure out what gems I wrote.

      Clarity in composition helps coherence in coding.

      The more carefully you code the less likely it is that you will ever need to re-read that code – precisely because it has fewer bugs in it. The discipline of a coding convention isn’t just for the benefit of the reader’s comprehension, but also the writer’s – at the time they’re writing it.

      But, anyway, wisdom cannot be taught – it has to be learnt.

      Therefore it may well be good advice to all new coders, to avoid use of coding conventions (except in collaborative works) until they appreciate for themselves whether they are useful.

    8. Dec 3rd, 2006

      Assaf

      This is actually something I learned from writing (not code). No matter how well you write, you’re still going to make a lot of mistakes. Editing is very important, I edit each post at least twice before it’s good enough to make it out there.

      And I don’t bother writing well at all. I only care about editing it well, before hitting the publish button. I focus on reading and judging how it reads.

      As a result I spent a lot of time editing what I write. But that’s no longer the case, because the brain picks up and improve and each editing makes you a better writer. You get that training for free.

      If you practice your editing, you don’t have to practice your composition. Even better, when you get in the habit of scrutinizing what you read, you can easily pick up ideas and tips from reading other people.

      Once I stopped practicing my composition skills, I became a much better writer. With text and with code.

    9. Dec 3rd, 2006

      James Taylor

      But what if you want non-programmers to read the “code”? How about finding the pieces of code you change all the time or explain all the time and coding them in something more accessible like business rules.

    10. Dec 5th, 2006

      GBS

      And what about programming on a team? Where there are 4 or 5 developers whose hands have touched (and will touch) the code base.

    11. Dec 6th, 2006

      Assaf

      other people’s code tend to be less readable than yours, so as a team you’re going to learn much more about writing readable code that other people can maintain, if you pay attention to your own hesitation reading other people’s code.

      and gradually you’ll end up converging on some common coding style, I personally always pick on the style of whoever wrote the most code.

    12. Jan 6th, 2007

      Coding conventions are a precondition to readability « AJ’s blog

      [...] About the same time I stumbled over an article that pointed to “Proof reading is more important than composition“. The authors opinion about readability is something to applaud. All the more irritating how wrong his conclusions are (just my opinion of course!). He deprecates coding conventions as being of adverse effect for readability. Having worked on software in teams of up to 80 developers and of different age (in terms of versions and maintenance impact) I am accustomed to read and evolve existing code as well as write code that has to be understood by other developers after I have moved on. And of one thing I am certain: Coding conventions are a necessary requirement for readability. Of course they are not sufficient, but do away with them and readability will follow shortly. All it needs is the next developer working on the code and using his personal coding style. This has to happen only once, no way to put the chicken back in the egg. [...]

    Your comment, here ⇓