views:

67

answers:

2

If you work with ColdFusion, you've probably gotten used to either line-wrapping or horizontal scrolling. Infrequently, this isn't so bad, but when three out of four lines in an existing code base are wrapped, sometimes twice, it becomes a huge readability hindrance.

A line such as...

<cffunction name="v_multiple_item" access="public" output="false" returntype="query">

can easily be broken up HTML-like into

<cffunction name       = "v_multiple_item" 
            access     = "public" 
            output     = "false"
            returntype = "query" 
            >

Which is fairly easy to automate with a macro in a good editor. What I'm more concerned with is interspersed ColdFusion and HTML, like so:

<cffunction..>
    <cfif..>
        <cfif..>
            <form..>
                <div..>
                    <table..>
                        <tr..>
                            <td..>
                                <cfif..>
                                    <select..>
                                        <cfoutput>
                                            <option>#stuff#</option>
                                        </cfoutput>

It's not uncommon that I see lines of code, with 8-sp tabs, that are line-wrapped during the whitespace.

Reducing tab width hasn't been enough. The code base is too big for rewrites/frameworkification to be an option. Left-justify everything?

Is there a simple, winning strategy for reducing line widths to a manageable level without further damaging readability?

+2  A: 

IMO, this isn't a ColdFusion specific problem, this is related to code readability in general.

My solution? I got a 28" monitor. :)

iKnowKungFoo
Absolutely, but I do feel like ColdFusion suffers a bit more than the rest because it's verbose, and its pseudo-XML is destined to be mixed with HTML.
mwc
+6  A: 

I don't want to get into a 'tabs versus spaces' issue here, but one thing you can do is adjust your tabs in the IDE. If you use tabbing, you could always make a tab be 2 or 3 spaces rather than 8 (or more). This will reduce the unnecessary whitespace, at least visually.

In Eclipse you can do this under Preferences > General > Editors > Text Editors. There's a 'Displayed Tab Width' there which you can reduce to your heart's content.

The real answer, however, is that this is just the nature of the beast. If you're going to intermix CF with HTML, and you want clarity, you'll indent your code.

Edit: Come to think of it, CFInclude just came to mind. Granted, this can also cause spaghetti code, but how things like this are often handled is with CFInclude. You can break reusable portions of CF code into cfm templates and include those. For a situation with a header, with many nested divs and pieces of CF logic intermixed, you'd be better off using a cfinclude anyway. So an index.cfm page that looked like your example might instead look more like this:

<cfinclude template="header.cfm">

<div>Some Content Here</div>

<cfinclude template="footer.cfm">

All of your wrapping is included in header.cfm or footer.cfm. You can even do includes within those templates as well, keeping things like the navigation isolated into a separate cfm page as well.

I'm not advocating that you go hogwild with includes, but it's a standard way of handling reuseable snippets of HTML/CFML in ColdFusion, and breaks things up into conceptual blocks that makes it easier to find what you're looking for.

Shawn Grigson
Cfinclude would provide a fairly neat way to separate out chunks of code without going for a full scale refactor. This would be ideal for chunks html and start you on the road to an MVC refactor. You could also look at separating conditions inside a function out into other functions
Stephen Moretti
I did mention changing tab width in my question (down with tabs, long live spaces!), but I hadn't considered using cfincludes as a temporary measure. Another measure I've used is vim's folding feature, which serves much the same purpose. They break when another dev updates the code, though. Thanks!
mwc