开发者

Managing line width in ColdFusion

开发者 https://www.devze.com 2022-12-21 16:08 出处:网络
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 ar

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?开发者_StackOverflow

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


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号