To close our tags or to not close our tags

It seems that an interesting debate about this within the ColdFusion community, sees that more people don't see the actual reason why it should be done.

Let me explain some more.

Anyone who has done any XML or strict validation of HTML knows that the tags should be closed, HTML isn't as bad but habit becomes habit with any of these tags that could have closing tags.

The worst thing that I can think of is debugging the code, and trying to decipher if the tag is alone tag or part of a block of code. For me the time that saves is worth more than an extra key stroke, most people that have complained to date have complained about the fact that it is a waste of an extra key stroke. Big deal, setup your IDE to to it for you, then it is no longer a waste of an extra key stroke. So that argument goes out the window.

Custom tags, whether they are run with a cfmodule tag or not. Really should adhere to using the is it a beginning or ending block. The reason behind this is that as we all know the code will run twice, and there is nothing worse than getting an angry support call because someone has been charged twice because a cfmodule or custom tag, was run twice because the developer wasn't aware that this code didn't check for the begin and end of the tag being run.

It might sound like common sense when you stop and think about it all, but Coding Guidelines are put in place for a reason. Me I am all for the introduction of closing the tag, because at a quick glance I know straight away that the tag is a one line tag and not a block of code. And that can make all the difference when you are developing, debugging and general development. I hate nothing more than sitting there and deciphering large blocks of code to figure out what is block of code and what is not.

If you can' think in that manner, it is something maybe you should really think about.



  • Robert Rawlins's Gravatar My mentality is very much the same as yours on this, I think it is worth doing for sure as it keeps things consistant with the markup I'm writing in XML and HTML which afterall is the whole point we use a tag based syntax within CF and also I agree with your point about making it simpler to read and asses code blocks.

    Rob
    # Posted By Robert Rawlins | 9/25/09 2:13 AM
  • Phil Yates's Gravatar Couldn't agree more, I think it's good practise. It gets you into the habit for when you are writing XML and also HTML/XHTML. Drives me insane when I read code that has open tags everywhere!
    # Posted By Phil Yates | 9/25/09 2:27 AM
  • David Boyer's Gravatar I agree, things look a lot clearer when you can see tags that are self closed and others that are open and have an end tag to look for. Although I always felt awkard around cfelse / cfelseif as they'll stick out a little when all your other tags are XML like.
    # Posted By David Boyer | 9/25/09 3:29 AM
  • Rob Dudley's Gravatar Close close close! And code your CF Modules accordingly.

    It makes code so much more readable and (until HTML5 is properly out anyway) it sets good muscular memory for marking up in XHTML
    # Posted By Rob Dudley | 9/25/09 4:57 AM
  • Adam Cameron's Gravatar Can you explain (perhaps with a code example) what you mean by this "trying to decipher if the tag is alone tag or part of a block of code."?

    Cheers.
    --
    Adam
    # Posted By Adam Cameron | 9/25/09 11:59 PM
  • Adam Cameron's Gravatar It'd be good if you *could* clarify the point I mention above, because it's the basis for your rationale, and it pretty much sounds like ballocks to me.

    There are situations in which one could argue closing a <cfinvoke> tag makes sense, because it can have an optional closing tag. And there are a few other tags like that. But MOST CF tags are not like that, so there is very little scope for ambiguity. There is never any doubt, for example, as to whether a <cfquery>; or a <cfqueryparamt> tag are the beginning of a block of code or not ("yes, always" and "no, never", respectively).

    So, sure, close tags which have some ambiguity about them, but you make no case for closing *all* tags.

    Because, basically, there's no *good* case to be had for doing it.

    --
    Adam
    # Posted By Adam Cameron | 10/1/09 1:00 AM
  • Andrew Scott's Gravatar Adam,

    Lets take built in tags out of the equation for a minute, and lets look at the likes of custom tags and modules.

    It is important to note that it can be argued that best practice, and common sense would prevail to make it mandatory to close these tags. Whether they can contain bodies of code or not. Without this, it makes it very difficult to know if I come across one of these at a glance whether it is part of a block of code or not.

    Now although I agree with what you are stating about built in tags, there are some that also adhere to this rule. Granted it isn't many, so with that in mind it would be easier to just adopt a best practice coding guideline to make all tags whether it could or should be closed as best practice in a coding guideline.

    Adobe does mention this in a guideline to ColdFusion programming practices, so its not just me who thinks this way. And anyone who has done XML/XHTML knows that it is strict enough to need to be closed.

    For example

    <br />

    Really doesn't need this to be forced to be closed, yet if you run any form of vlaidation over the code it will fail if you don't put it there. So there ColdFusion side of it becomes a habbit and best practice to do so.

    I obviously didn't make the point of habit in comparison with other languages clear enough.
    # Posted By Andrew Scott | 10/1/09 8:49 PM
  • Adam Cameron's Gravatar FIrstly, completely fair enough re custom tags.&nbsp; The ambiguity is definitely possible there.

    However you seem to be missing the point that in XML and XHTML it's a *requirement of the specification* that all tags must be closed.

    This is *not* the case in CF. It's *absolutely* not the case with CF.&nbsp; Not all tag based implementations have that rule (HTML is an obvious example). Most CF tags DO NOT HAVE a closing tag. Try this:
    <cfset foo="bar"></cfset>

    (don't forget that <sometag /> is an abbreviation of <sometag></sometag>)

    You get a compile error. Why? Because <cfset> doesn't have a closing tag. It's not that it doesn't do anything, it's that IT DOESN'T HAVE ONE.

    What you're doing when you do this: <cfset foo="bar" /> is not "closing the tag", all you're doing is putting an unnecessary slash in your code.

    Also consider what the CF compiler does when it sees <cfset foo="bar"> vs <cfset foo="bar"/>. Nothing. It just ignores it. It does not go and process the "closetag" part of <cfset>. Why? Because there isn't one. Because <cfset> doesn't have a closing tag.

    So closing a CF tag which has no close-tag implementation is:

    * generally no clearer in intent (other than exceptions like <cfinvoke>, in which case common sense dictates one could possibly close the tag);

    * actually - stictly speaking it obscures intent, because it suggests in other situations there MIGHT be a closing tag to look out for (in other people's code who don't slavishly, and IMO moronically close their tags);

    * serves no purpose;

    * is actually illegitimate as far as the language goes (ie: there very much is no </cfset> tag);

    * based on a false premise that there's any justification to do so because tags intrinsically are meant to be closed.

    Some people have said "oh, I just got in the habit when I was doing all that XHTML". Oh *please*. Are you really so incapable that you can't remember when you're doing XHTML and when you're doing CFML? If anyone ever admitted that to me, I'd be questioning whether they're in the right job (actually: no question necessary: they're not in the right job).

    This leaves "following the Adobe coding standard". I think the Adobe coding standard needs to be changed, because it's currently flawed. I think one should definitely follow a coding standard, but one definitely should not follow that particular one until it's corrected.

    --
    Adam
    # Posted By Adam Cameron | 10/2/09 3:17 PM
  • Andrew Scott's Gravatar @Adam - I think you have missed the point, the point is that it is a coding guideline only, guiding guidelines are put in place for a reason. In this case I use it in this manner NOT because I have to confirm to XHTML standards of being strict adherence, but because it is habit.

    Nobody has come out and stated that we have implemented these coding guidelines because we want it to conform to strict XHTML coding at all. Its only you who has come and made that assumption.

    An IDE can be setup to do the job for you to close or not close, and its no big deal whether I have to add one extra key stroke or not, all I care about is that the code is readable and quickly able to be debugged. That's why we have these coding guidelines in the first place.
    # Posted By Andrew Scott | 10/4/09 9:00 PM