Wandering around the interblag lately, I've come across a number of "conditional comments considered harmful"-type posts. I'll not name names, as some of them are from developers I quite respect… and I'd rather respond to the general arguments than take aim at an individual.
There are four major arguments I've seen:
- conditional comments impede separation of content and presentation
- conditional comments are not standards-compliant
- XSLT doesn't "play well" with conditional comments
- CSS hacks are superior to conditional comments because they do not suffer from the above problems
I find the first argument to be by far the most compelling — there's no denying the fact that the use of conditional comments adds non-content markup to a document, even when only used to reference external stylesheets. However, this seems to me more of an anthill-scale problem than a mountain-scale one. Outside of lab conditions, non-content markup is simply a fact of life. Onion skins, container <div>s, zebra striped tables, and other techniques ensure that few web sites achieve perfect separation between content and presentation. The reason for this is simple — HTML is not a pure content language. Trying to treat it like one is sure to painful and unfulfilling; better to use XSLT to transform pure content markup into HTML, adding conditional comments and other presentation markup as it goes.
Moving on to the second point, standards compliance. I feel it's a little disingenuous to call conditional comments a violation of standards. It's the browsers which recognize them which are non-standard (violating the instructions that comments "have no special meaning" and should be "ignored by the parser"). The contents of comments, however, are intentionally unspecified in standards… and considerable value has been found in placing data outside the scope of standards in comments. Javadoc and Subversion keywords, to name some popular examples. Plus, the nonconforming browsers in question are used by fifty to eighty percent of users (depending on who you ask). Steadfastly refusing to make concessions to their irregularities, while perhaps laudable in principle, risks alienating a large chunk of your potential visitors.
Now, XSLT. This one is surprising, as I've never had problems with it, myself. Here's the applicable snippet from a site I recently created for a friend:
<xsl:comment><![CDATA[[if IE 6]>
<link rel="stylesheet" type="text/css" media="screen,print"
href="style/default/style-ie6.css"/>
<![endif]]]></xsl:comment>
<xsl:comment><![CDATA[[if IE 7]>
<link rel="stylesheet" type="text/css" media="screen,print"
href="style/default/style-ie7.css"/>
<![endif]]]></xsl:comment>
Which finally brings us to conditional comments vs. CSS hacks. Again, I feel this is fairly clear: CSS hacks exploit unintended behavior. Conditional comments, on the other hand, are intentional. I'd much rather hitch the usability of my site to a misfeature than a bug — the misfeature is likely to be more predicable and have better longevity.
In the end, sure… we'd all like to write beautiful, standards-compliant code and have it Just Work in all browsers, but that simply isn't the world in which we live right now. In the future, perhaps, we'll be able to do without but for the time being, I'd call conditional comments by far the lesser evil.

0 comments:
Post a Comment