Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Saturday Speakers – Thoughts when schedulingSQL Saturday #118 Wisconsin: They Came, They Saw…Did They Tip Cows? »
    comments

    This post really started over fifteen years ago, if only I had known it then. You see, once upon a time I wrote a piece of code with nonsense variable names and, another time, a bunch of dynamic, inline SQL, and there was that time I tried to write half the program on one line. And lets not even get started on error messages....

    Then I got better.

    But it took a while.

    And I'm not done.

    Code is for Other People

    We don't write code for the computer. We write code for ourselves an hour later, after we we have fallen out of the zone and have to debug it. We write code for 2 months from now on a Friday afternoon when it's the only bug keeping us from our weekend. We write code for 2 years from now when the axe-wielding psycho of a maintenance developer hits the blame button to see who volunteered to be his newest, bestest friend. Or trophy. Probably trophy.

    When we're producing code, we have to be mindful that there will be a person consuming our code and stop focusing on just getting it out of our head. When we come back to this code in 6 months we want to be able to read it easily, be able to safely make assumptions about elements in it, understand the intent as well as the concrete implementation, and do so with the least amount of re-reading possible.

    Be Consistent


    Code standards tend to get a bad rap, especially among younger developers. They're seen as unnecessary extra work or as limiting creativity. I remember thinking this way at one time and agree that over-prescription can be wasteful (but c'mon, over-prescription of just about everything is bad, that doesn't mean we can't take something for our headaches).

    The majority of code standards boil down to naming things consistently and being consistent in how functions and whitespace are structured. We do this so we can make quick assumptions about the code as we scan a file, identifying scope or type of variables, locating blocks of code, determining level of nesting, etc. Sure there is some extra work when we initially adopt a standard, we lose some time as we get used to new naming patterns and structures, but once we get used to the standards it becomes just the way we do things, no more.

    And as far as creativity goes, unless you're taking part in a code obfuscation contest, focusing on being creative at the code structure and variable naming level is a sign that you've lost sight of the bigger picture, namely that you have an entire program to be creative with. Or you're too busy trying to express your inner hipster, one of the two.

    Ugly Strings are Ugly

    I'm not a fan of inline SQL. It tends to be written poorly, doesn't get tested for syntax errors until the first time you hit that code path, can be nearly impossible to locate when you decide to change a field name in the database, and often gets written in a solid block of unformatted text.

    And the same happens with varying degree in HTML, LINQ, Perl, ... really anything that resembles 50 lines of content without a carriage return.

    Even with guidelines for structuring our code, we need to be mindful of the blobs we produce. These blobs are difficult to read, adding to the level of frustration and time wasted when a future developer has to work with our code (Who wrote this garbage code!? Oh, me, argh). If the blob is truly a string, it deserves good formatting and consistent capitalization just as much as our code. In fact, I would say being consistent is even more important here, where color-coding and syntax checking tends to be unavailable.

    And it's ugly. We may have saved 30 seconds and got the job done, but it's not worth taking pride in.

    Well, I Can See What You Were Trying To Do...

    I once reverse engineered some business logic out of a chemical cooker simulation program written in the early to mid 80's, revised 3 times by other developers, and utterly lacking in comments. There were no unit tests, there was one comment in the entire codebase (and it was wrong), and I often ran into cases where I could very clearly see that the developer was doing math, but couldn't figure out why he had chosen to use set VT = f(VT+1) when everywhere else we were calculating VT+1 = f(VT).

    Why were we projecting back in time with that one variable? Turns out someone had swapped the naming scheme for current and future state variables, what a pain that was to figure out. Probably saved them a few seconds to not correct it and a few more not to comment their intent, but it cost me two full days to back into what they were intending to do and determine it was a spelling error. Who knows what it cost maintainers in the 20 years in between.

    There are two things we need to know when we look at a piece of code: what does it do and what it was it intended to do. We can determine what it does by reading the code, but there are many cases where we have to guess at what it should be doing. I've learned to start with the assumption that implementation and intent aren't in agreement (all the while hoping I'm wrong).

    Good naming schemes that identify intent, unit tests, function-level comments, good commit comments. These may seem unnecessary now, but 6 months from now they will be critical, whether it's to maintain a piece of code or copy it for reuse.

    Error Messages Make Hulk Angry

    No one likes getting an error message. An error message is the sign that no matter what we were doing, something has gone terribly wrong and we are now a whole lot further from being done than we thought we were. Obviously what we need in this tense moment is an incomprehensible or sarcastic message from the failing application to really put our mind at ease.

    No?

    How about this then. We develop some software, send it out in the world, and one day receive a call from an annoyed end user about the error they are receiving. The message simply says "Null reference error", maybe with a brief joke about cheese or a reference to a technical manual for the definition of null reference. Because we all love those "oh crap, I don't even know where to start" moments.

    No?

    Having been on both sides of this issue, I know neither is fun. In fact, I'd love to stop learning that lesson any time now. Error messages don't have to increase our stress levels this much. Unfortunately there is a tendency to add the least amount of time for error tracking, compound that with poor error messages, then just live through the frustration when the situation is kicking our butts later.

    Better error messages aren't that difficult, though, we just need to put more thought into them. We need to keep those two users above in mind when we're crafting our message, and keep in mind the situation they will be in when this message matters. As the end user, we need help staying calm, information on how bad the situation is, a description of what our next steps should be, and the knowledge that everything is under control. As the developer, we need information about the root cause and all the state information we can get our hands on, to speed up detection and resolution.

    It doesn't really take longer, especially if we invest in our error reporting infrastructure. Most applications could afford to spend a week or more here, given how many hours/bug of debugging time we will end up saving in the near and long term. But more importantly, unnecessarily upsetting users and developers during an already stressful moment is a double loss that will hit us with every bug, exaggerating an already negative situation.

    There's More

    There's more to this than consistency, clarity, explaining intent, and providing useful error messages. But it all starts with being mindful that when we code, we are not just writing something to jam through a syntax check and successfully compile. The code we write needs to be clear to the future reader and allow them to understand it with the least amount of re-reading and cross-checking. It needs to be aware that it has an audience, both future developers and end users. Good code is judged, not just on it's performance characteristics, but more importantly on it's ability to age gracefully, minimizing maintenance costs and the number of people it upsets along the way.

    About the Author

    User bio imageEli delivers software and technology solutions for a living. His roles have included lone developer, accidental DBA, team lead, and even unintentional Solaris consultant once. With experience in adhoc, Lean, and Agile environments across NSF grants, SaaS products, and in-house IT groups, he is just as willing to chat about the principles of Lean or Continuous Delivery as he is to dive into Azure, SQL Server, or the last ATDD project he created.
    Social SitingsTwitterLinkedInHomePagedeliciousLTD RSS Feed
    InstapaperVote on HN

    9 comments

    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) Hip Hip Hooray.

    I wish every developer thought this way when coding. My first gig out of college had me working on an old Clipper based system. Most of the code was straight forward, but there was this one block of code that I considered "Magic" because it worked, but was impossible to understand, and equally impossible to modify.

    Eventually it was necessary for me to modify this code (to add new functionality). It took a lot longer for me to do this task than it should have. A couple of comments would have made a huge difference.
    04/24/12 @ 06:47
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Great blog!
    04/24/12 @ 12:09
    Comment from: chopstik [Member]
    chopstik I will readily admit that I've been guilty of this in the past (as I'm sure all of us have when we first started). Nowadays, I find myself pretty annoyed when I see some of these practices and, if they are non-beginners, less patient with the developers themselves. Go figure...

    Great post, Tarwn!
    04/25/12 @ 18:45
    Comment from: Eli Weinstock-Herman (tarwn) [Member]
    Eli Weinstock-Herman (tarwn) Thanks guys. I find that even now I find myself falling into bad habits or finding code from 6 months ago that puzzles me, but I think this is probably like professionalism, less a place we arrive and more something we have to keep striving towards.
    04/26/12 @ 06:20
    Comment from: Fadi (itoctopus) [Visitor] · http://www.itoctopus.com
    Fadi (itoctopus) Your part on useful error messages should be read by Microsoft. Well, come to think of it, most programming languages out there provide really bad error messages.

    If programming languages provided better error messages and better tracing, then programmers wouldn't have to implement their own error libraries.
    04/26/12 @ 08:19
    Comment from: Aaron Throckmorton [Visitor] Email
    Aaron Throckmorton I am completely guilty of everything you mentioned. Even after years of coding. Mostly I think this is because I am always learning. So the code I write today when learning a new language or concept, I look at in months and realize I did it all wrong!

    I will definitely be keeping this stuff in mind (moreso) now. Good article - thanks!
    04/26/12 @ 09:03
    Comment from: mike [Visitor] Email · http://www.mikepope.com/blog
    mike Yay. In my job, I'm sort of in the middle of the fray -- I edit text, including UI text, including error-message text. Many times I've been handed error message to edit, and I'll have to go back to the developer and ask "Can you tell me some more about what exactly is going on when this error message is displayed?" And what's surprising is how often that can lead to me eventually saying "So, what you just explained to me ... can we just say that in the error?"

    And double yay for noting that part of an error message should be a suggestion to the user about what to do next. If I'm a user faced with an error message, frankly, I often don't care what the nature of the error is, really -- I just want to know what I should do now. (Twitter and FB have been pretty good about this.)

    Finally (sorry to carry on here), standard advice for error messages is to not blame the user, implicitly or explicitly. I still often see poorly phrased error messages. If the POS card reader at my supermarket can't read my card, it tells me "Bad card swiped." This makes me all, like, screw you, you stupid machine, you're the one that can't read it. If nothing else, at least apologize to the user for the inconvenience of having suffered the error. :-)
    04/26/12 @ 11:52
    Comment from: Eli Weinstock-Herman (tarwn) [Member]
    Eli Weinstock-Herman (tarwn) @Fadi: Hah, "Null reference error" isn't informative enough for you? I agree, we tend to think purchased or Microsoft libraries should be the shining example, but that's often not the case.

    @Aaron: You should see the code for the LessThanDot site, PHP as written by a bunch of developers that rarely use PHP. Languages tend to have their own conventions, so beyond the general stuff, there's often all kinds of conventions that really polished developers will use with a language that those not familiar with it wouldn't even consider (like bootstrap files in PHP).

    @mike: Good point on not blaming the user, I should have included that in the post. If anything, we should apologize that the user lost time/data/whatever in finding it, but also thank them for finding the issue, communicating it to us, etc. Whether it's bugs, external dependencies, usability..something caused the user to go down a path we didn't want them to go down and they have provided us an opportunity to make it better for future users.
    04/27/12 @ 07:48
    Comment from: Stefan [Visitor]
    Stefan @mike:
    Very good point about not blaming it on the user. I always feel offended when after a blue screen, on the reboot I get told that I should use the 'start' button to shut down the computer, not just switch it off... Am I to be a moron every time the OS crashes?

    @Fadi & Eli:
    When I was reading Fadis comment I was more thinking about 3 page error messages caused by template name obfuscation...

    @Eli:
    Thank you for this splendid article. I'm sure I'm guilty of most if not all the bad things you mentioned, but nowadays I do try to avoid them, being well aware just how hard it is to read code, even your own, after some time.

    Furthermore, whenever I have to dig in some legacy code, I make a point not just to fix it, but also insert comments about what I've found, so the next guy coming to this place will have an easier time at it. In retrospect, that next guy is usually me, so I'm doing myself a big favor, in fact. :o)
    04/27/12 @ 09:31

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)