- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Agree with you, the justification seems quite plausible.
BTW, won't it be easier to just iterate through the 'staff' and extract the first letter (thus we'll get all and only existing letters)?
Admin
Probably 'easier' in the sense of 'easier to figure out now why it really is this way, instead of just hacking around it and hoping it does not cause some horrifyingly hard to track down bug in 3 months.'
E.g. "Stop and think! There's plenty of time to fuck up later."
Admin
This is brilliant coding. The for loop option doesn't cover the possibility of the English alphabet changing, while this XSLT masterpiece will. I don't see a WTF.
Admin
one word...why?!
Admin
Admin
What interests me is that the guy in question is apparently derided as an "old-style hacker..." And now, back to your nutritious and delicious breakfast of XSLT.
Admin
It is possible to use JavaScript in XSL for some really complicated code where you are using JavaScript functions to do calculations on a node value. So you could do loops that way and avoid the XML for the alphabet.
XSL is a powerful tool for formatting and processing XML data feeds. It is also remarkably portable. I've used it with content management systems like DotNetNuke and Joomla and for ASP.NET web parts. Once I've worked out the XSL style sheet I can use it with a variety of web application frameworks.
Admin
That's a terrible idea. There are some major things wrong here.
*) How do you know that every letter is represented by the SURNAME column's first letters? Someone might insert a new one later, for instance.
*) That SQL query is among the worst perfoming choices: SELECT DISTINCT LEFT(LTRIM(surname),1) AS Letter FROM [Staff] WHERE [IsActive] = 1;
The DISTINCT means the rows must be sorted and pared. I discarded the useless ORDER BY that does the sort over. (Your DBMS may vary.)
The LTRIM(surname) shows that there may be random spaces before the SURNAME. This means a SUBSTRING(...) function INDEX won't work. (Always run TRIM(...), or any format processing, on input!)
This STAFF table represents many people. It could have several thousand rows. The more rows the worse this query gets.
Admin
I agree. No one will be laughing at this code when the US finally pulls its head out and converts to metric, including finally implementing the Decabet.
Admin
The question of if xml/xslt is at all appropriate in this case got a lot of discussion already, and still no one of you considered that it might have been part of the requirements. Maybe it was intended as a xslt skill test.
I'm most surprised no one here commented on the actual mistakes in the code, however. Perhaps no one of you understands xslt?
He tried to handle the case of no data for a given letter, but failed miserably. The definition of the "found_alpha" variable would make it a result set always containing a text-node "false", followed by as many repetitions of "true" as there are data entries starting with the specific letter.
He must have figured out that this variable doesn't work as he intended, because this debug code
never ever printed out anything. And so he outputs the letter link unconditionally, without checking his flawed variable.It is still curious why he didn't totally remove the variable after he gave up on making it work and stopped using it though... would've made the code much less of a wtf.
Admin
There are more WTFs here:
Is $found_alpha ever equal to "true"? $found_alpha always starts with the text "false", and then it looks like "true" might get concatenated on the end multiple times depending on the document structure: "falsetruetruetruetruetruetrue".
If $found_alpha was equal to "true", the list element would contain the word "true". However, since $found_alpha is never equal to "true" (it always starts with "false"), this never happens.
As far as I can tell, the entire $found_alpha business doesn't actually do anything. This stylesheet doesn't actually filter out letters that have no associated surnames.
Admin
wtf, you beat me to it.
Admin
Assuming you have this data in a database, rather than an XML file, the following would probably be the most efficient way to do this in CFML. I don't have a CF box handy, so this might be completely wrong...
<cfoutput query="qgetstaff" group="fl">- #fl#
<cfoutput query="qgetstaff">- #firstname# #lastname#
</cfoutput>
</cfoutput>Admin
sounds like someone I used to work with. "why do simply when there is a much more complicated way to do it?"
Admin
To handle adding/removing letters, in (unoptimised) Javascript
var alphabet = "abcdefghijklmnopqrstuvwxyzþ".split(""); for (i=0; i<alphabet.length; i++) { var ch=alphabet[i]; //do stuff }
Admin
And you know what you'll get? Alphabet SOAP.
Admin
'A'.upto('Z'){|l| puts "<a href="##{l}">#{l}"}
Ruby for the win.
Admin
Although using very little code, that looks ugly as hell. I'd put up with two more lines for something that resembled nothing like that.
Captcha: cogo, this is a no go
Admin
'A'.upto 'Z' do |letter| puts '' + letter + '' end
Better?
Admin
The DISTINCT means the result set will have no 2 rows the same, how this is achieved is down to the DB, so it's probably better to leave the ORDER BY in, then you're guaranteed to get what you expect. (Let the SQL optimizer work out it's done the sort, that's what it's there for.)
IMHO always better to specify what you want as precisely as possible with things like SQL.
But yeah, that query does suck donkey balls. :)
Admin
Admin
This XSLT is braindead -- You can do this in a few clear lines of XSL:
<xsl:template match="node"> <xsl:if test="starts-with(document($staff)/staff/profile/surname, .)"> <xsl:value-of select="."/> </xsl:if> </xsl:template>
Admin
First, as a CF programmer, CF has a lot of looping available; someone earlier was complicating things with DB queries, a simple cfloop over a list of letters would have worked much easier.
Second, "in the job market yet" is the biggest problem I have with Adobe (who bought ColdFusion from Allaire) ... they don't really advertise the advantages of this language. It's been around for over 10 years! I found out about CF about 3 years ago, and love a lot of features. Allaire was way ahead of their time.
Oh yea, back on topic ... um ... I wonder if this was a non-CF programmer trying to back-fill this functionality without having to use CF? Otherwise, a true WTF.
captcha: letatio (isn't that illegal in some countries?)
Admin
Personal preference? I like it that the code looks pretty damn clean and reads like a sentence. Maybe not everyone values that, but I do.
Admin
Formatted this way, reminds me that I could probably write a very similar-looking shell script to output the same thing to a text file via "echo" statements.
Admin
To say that it's a good solution if the author was constrainted by company policy or a boss's directive is beside the point. If something is a stupid idea, it doesn't turn into a good idea because the boss said to do it this way. It just means that it's the boss who is being stupid rather than the programmer.
Admin
Covers all the basis in the event that the alphabet changes in a non linear way, or someone demands you support a non english language...
Admin
to a hammer everything looks like a nail
Admin
Their XSL code is pretty wacky, but there is a real reason to put the alphabet in XML and then run an XSL transform against it to obtain the tags.
If you were wanting to output these links, and you knew that the code for the tags would always be the same, then yes, you could just go with the for loop plan.
However, what if the content of these tags were subject to change? For example, what if you wanted to add a style to the link? Or if you wanted to make all of your links _target="_blank"?
If you did this programatically through server-side code, you'd have to update the codebase and then recompile, redeploy, etc. However, using XML/XSL, you could just change the XSL stylesheet so that it outputs the tags with your styling info or target data or whatever.
Of course, I don't know why someone would need such a flexible way to output alphabet links, but I don't doubt that such a reason could exist.
Admin
First, "update the codebase and then recompile, redeploy, etc." may have been a scary proposal 20 years ago, but people REALLY need to get over this and stop perpetrating horrors like this in the name of avoiding code builds. With scripting languages, there is no difference whatsoever between changing code and changing an XSLT - which should be no suprise since that's really just another scripting language as well. And even with less "agile" languages like C or Java, builds can (and should) be automated so that all you have to do is press a button.
Admin
The REAL wtf is how the description doesn't actually describe what the code does. (The other wtf is that nobody knows XSL anyway, so nobody noticed.)
Admin
How about (XSLT 2.0):
<xsl:template name="alpha-nav"> <xsl:param name="start" select="65"/> <xsl:param name="end" select="90"/> <xsl:for-each select="$start to $end"> <xsl:variable name="char" select="codepoints-to-string(.)"/> <xsl:value-of select="$char"/> </xsl:for-each> </xsl:template>
Don't take it out on XSL...
Admin
[quote user="brazzy"][quote user="Alonzo Meatman"]If you did this programatically through server-side code, you'd have to update the codebase and then recompile, redeploy, etc. However, using XML/XSL, you could just change the XSL stylesheet so that it outputs the tags with your styling info or target data or whatever. If you see any difference at all, you have much to learn.[/quote]
Well, as much as I appreciate your interest in my development as a programmer, I have to disagree with you.
I see that the developer of this "WTF" uses ColdFusion, which is indeed a scripting language. So for this instance, perhaps your point is valid.
However, I'll hit you with this hypothetical - what if they were using a compiled language like C# or Java? Furthermore, what if this code was deployed for, say, 12 different sites? And what if these 12 different sites all used different stylesheets and formatting? Furthermore, what if the company wanted the style/formatting to be changeable by someone who wasn't a programmer, i.e. a graphics designer?
In that case, hardcoding the details for the tag would in itself be a WTF. This is one of the great uses of XSL - it allows you to change presentation-level details without requiring you to change your code.
Once again, in this specific instance, it may be a misuse of the technology. But I hold that the concept itself is sound.
Admin
Reminds me of when I saw some code like this one time:
bool statusFlag;
...
switch (statusFlag) { case true: <some code here> case false: <some other code here> default: throw new exception("Unknown Status"); }
And I figured they just wanted to make porting to multi-state quantum computers easier...
Admin
A third real wtf would be why isn't there another set of tags for lower-case alphabets? :)
Admin
Well it looks really stupid, but if you're using XSL transformations to do it every way you'll try to iterate over the alphabet vill be a bit hacky. You could pass a string containing the alphabet as a parameter to a named XSL template that that calls itself with the string as parameter but removes the first letter each time. That way you would save yourself from having an extra nodeset just to iterate.
Admin
Admin
WTF? Would you seriously let a graphic designer loose on an XSLT implementation as described above? Best of luck.
Hint: the correct answer is CSS
Admin
Okay, fine. Leave out the part about the graphics designer. You still have 12 different sites that need special customization. Do you really want to put all of that into your DLL ?
And I agree, CSS is part of the answer. But if you go the XSLT route, you don't have to hardcode any of your CSS classnames or anything like that.
Look, I'm not defending this guy's code. I'm just saying that there COULD BE a good reason to do this, although it's more than likely that the developer just didn't know what they were doing.
Admin
This is the first time I've ever felt compelled to comment.
The XSLT isn't great XSLT, but what you're missing is ... it's XSLT.
Seriously, that's what it's like. It's not like the languages you're used to. It doesn't work the same way. If you need to iterate through the letters A to Z, creating an XML file containing them as nodes is perfectly logical. If the only tool you had to do the job is XSLT (1.0), this is (roughly) what you'd get.
So, it boils down to you all saying "the real WTF is the entire XML/XSLT platform". Which you're entitled to do. But ... that's not what this site is for, is it?
Admin
I would want that code in the build in some shape or form, so it's defined, under source control, and testable. It's something that would definitely belong in the application's presentation tier, but I'd rather that tier was written in the same language as the rest of the application, and not in XSL. If you have something that's different enough not to be manageable through CSS, or modifying the app's output based on settings from db/config file etc., you probably need to either rethink your app's architecture, or accept you have 12 different presentation tiers.
While you can make XSL do almost anything you choose, if you try hard enough (it's Turing complete), you are probably going to come up with an unmaintainable hairball in comparison with a bona fide programming language. Save XSL for cases when you have some XML already, and need to transform it, but it's an unwieldy choice in most other circumstances.
More generally, it's unrealistic to assume you can abstract presentation far enough away from logic through templating languages (XSLT, Smarty templates, or whatever else) to make it safe for idiots to edit. They either have enough rope to hang themselves with, and do, or can't do anything useful, so you end up with work being passed back to developers.
Admin
:D
Admin
Admin
Actually we don't have to imagine - a quick Google for "Muenchian method" turns up the relevant links (Yup, I've been there).
Turns out one of the recommended techniques for doing this is something called the reverse Muenchian method (with substrings) - http://www.stylusstudio.com/xsllist/200201/post21090.html.
I'll wager that in trying to understand that particular bit of insane XSLT recursion our fair proggrammers head started hurting something fierce.
So then after a nice lie down, and a refreshing cup of tea they came at it again.. and thought hey I know a simpler way..
In fact, and this is a direct quote from the XSLT List above, they may have thought... "Another way would be to hardcode your alphabet as a set of nodes .. [and] then just iterate over these, or iterate over the nodes you already have with a test whether its initial letter (normalized) is = $alphabet. It may be a little less cumbersome than recursion."
Yeah. Way less cumbersome than recursion when "for i<26: asc(i)" is not even in consideration.
And for that we shouldn't blame our intrepid programmer, rather we should blame the insane bastard child of XML -- XSLT - the root of many a programming evil.
Admin
WTF is a "US alphabet"?
Admin
The English alphabet, but written bigger and spoken more slowly.
Admin
Admin
Which would lead to a wrong result in the german alphabet because the umlauts are integrated (whereas e.g. in swedish they are at the end). There is a reason why every framework has locale-dependent sorters.
Proposing a loop over the ASCII codes and hacking this loop to work for specific alphabets afterwards is the real WTF. Externalizing the data is a reasonable solution, even if the way it is shown here may not be the most elegant.
Admin
Admin
A shortcut for saying that these problems mostly arise with US-made software where people think that iterating over _A_SCII codes will give you a list of the alphabet.