Code Formatting, How Do You Roll?

There’s something that has been bugging me for quite some time. I dont mean like a little nagging annoyance, Im talking about an inch long splinter in the bottom of your foot and having to walk around all day on it kind of bugging!
Code formatting. There I said it.
Now call me OC or what have you but yes I admit Im quite anal when it comes to code formatting. There is nothing I hate more about coding than to open up someone’s code and see poorly formatted code. Is there a reason to try and cram as much crap on one line as humanly possible when coding? What is it about some CF coders that makes them code this way?
I try to make my code as readable as possible. This includes breaking at a reasonable column so as not to run too far off the side of the screen. But why do some people code so that it takes a 30″ widescreen monitor to read the whole line?

And whats with code like this:

Username:


Im not sure if BlogCFC will wrap that code or not but Im looking at an application right now that has that very code in it, and its all on one line with the exception of the closing table tag. Now is that really necessary?
Isnt this much more readable?

Username:

Sorry for ranting but its just been that kind of day!! What are your thoughts on code formatting? Do you see alot of this sort of thing? Are you guilty of it yourself? If so, why? Im really curious.

12 thoughts on “Code Formatting, How Do You Roll?”

  1. I feel the same way about formatting code. It is absolutely essential that code be readable if there is ever a hope that it will be understood, let alone fixed or changed to support a new feature or requirement.

  2. It’d be even more readable if it was coded with proper tags, not tables and spans!

    <div class="field">
    <label for="username">Username:</label>
    <input id="username" type="text" name="username" size="20" value="#error.username#"/>
    </div>

    And of course, you can take formatting a step further, which can often help to further improve readability…

    <div class="field">

    <label for="username">Username:</label>

    <input
    id = "username"
    type = "text"
    name = "username"
    size = "20"
    value = "#error.username#"
    />

    </div>

  3. No, that’s crap too.
    those tabbed input attributes need to be single-spaced instead.
    I agree though, that table, tr and td elements need to be on their own lines. TD elements should be inline with their content for inline elements (inputs and spans), however, TDs need to be alone on a line when their nested elements are block elements and/or nested more than 1 deep.

    Actually, I would go further and say the original isn’t only poorly formatted, it’s also poorly coded.

    <td><span class="label">…
    should be
    <td class="label">
    Or better yet, no tables and do
    <label for="username">…

  4. I agree. I make sure code format is readable before I try to work on it. Several times, I have found that the process of formatting code to make it readable reveals the problem I am being called in to solve.

    One of my clients used to complain that I wasted time with formatting. Once I got her to try it a few times, though, she was hooked. Now she is nearly as neurotic about code formatting as I am.

  5. I prefer something more like the following (it is displays correctly)

    <table width="90%">
       <tr>
          <td><span class="label">Username: </span></td>
          <td>
             <input 
                type="text" 
                name="username" 
                size="20" 
                value="#error.username#">
          </td>
       </tr>
    </table>
    
  6. Agreed Russ, sometimes it’s almost like the developer has gone out of their way to make the code as ugly as possible for the next guy to wade through.

    Just yesterday I was looking at some legacy code (which I don’t know who wrote) and was getting muddled by the lack of indending with nested if/else if/else blocks.

    Don’t people take pride in their work? And call me crazy but "being too busy" is simply no excuse. Argh I could go on for days…

  7. Is it necessary, yes and no.. Recently, I’ve spent alot of time optimizing my site.. This involved using YSlow to analyze the page load, and address issues accordingly.. One of the main things i’ve come across is minimization and it’s impact on the document.. It really does make a big difference at load time.. Is it really necessary, of course not.. But it does have it’s own value..

  8. @ Peter – I agree, proper tags would be great but hey, we cant all have perfect clients can we? lol

    @Nathan – Seriously? Single spaced? I dont think I have ever worked with anyone that used spaces for indention rather than tabs. Not sure I could handle a single space, maybe 3 or 4 though.

    @sakri – Hilarious!

    @Bill – I havent done much with ySlow and minimizing yet. Its definitely on my plate for the near future though!

  9. Ya, I’ve loved what I’ve acheived by cutting out the fat.. I’ve since created some cf stuff that will basically load all my JS and CSS files from minimized copies, and serve it via 1 HTTP request.. this has shown enormous benefits..

    On a side note, you may want to check ponder a blog i made yesterday just for fun.. http://billieblaze.thevillage.com/index.cfm?fuseaction=blogs.display&id=7&blogID=8347
    I’ve gone a step further in trying to optimize.. The blog details how to generate thumbnails on the fly and serve them from cached areas in apache..

Leave a Reply

Your email address will not be published. Required fields are marked *


*