- 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
"Fr1st".toLowerCase()
Admin
Y U NO ESCAPE CHARS IN CODE? :P
Admin
Dat validation
Admin
Admin
For added Lulz, this post only makes sense if you read the HTML source!
Admin
At least there will be no debate about what TRWTF is.
Admin
ESCAPE FROM THIS MADNESS
Admin
Time to go write some really WTFy code containing malicious Javascript and wait for it to be published.
Admin
Admin
What C family language supports arbitrarily closing a paran with a brace? Or are we just making WTFs up as we go along?
Admin
But the PRE tag shuts off the HTML interpreter, doesn't it?
Hint: They're trying to train us to view source on every article.
Admin
Someone forgot to escape their "<". It's been fixed, and for good measure, run through a syntax highlighter so that you can see the WTFness in the code IN COLOR.
Admin
There honestly should be such a tag. There should also be a tag that allows you to pass its contents to a different interpreter, thus making it easier to inline binary data.
Admin
But sometimes people forget that "pre" in HTML does not allow you to use angular brackets in HTML directly (without encoding them as their corresponding HTML-entities).
Admin
And regarding the article, this isn't a WTF. Regular expressions are expensive and difficult to maintain!
Admin
So you look at your code, and you think: "hmmm... maybe I shouldn't call toLowerCase() more than once on the same string".
Bang, along comes Donald Knuth and says "premature optimization is the root of all evil!" ;-)
Profiler, anyone?
Admin
Parsing HTML with regular expressions? That never goes well.
Admin
So the toLowerCase is clearly a WTF. Comparing the text to every known tag is at best a borderline WTF. There are more efficient methods, but they are more complicated to implement. I can think of:
Admin
Forget carbon emissions; this is where global warming comes from people!
Admin
As a PHP newb, I'd be thankful if someone could name one of those "reliable libraries a developer could use to do the heavy lifting." A simple one, please.
Admin
Admin
Once you figure out that your code crashes, or takes a day to process a large page, the optimization is not premature anymore.
Admin
I actually like the first test in the sample given: it fires on all tags starting with "<a", not only the anchor tag.
Ah well, the "Do a test involving the tag"-Test will probably weed out applets, areas and the like.
Admin
But storing the result of toLowerCase() in a temp var and working on that variable would be :-)
Admin
FTFM
Admin
Slow yes, but who here thinks it would take 24 hours to process a single page?
Admin
Admin
Admin
It's worse than that.
Every time a tag is found the entire page is converted to lowercase.
Admin
It's worse than that - every time a tag is found on the page the whole page is converted to lowercase. 70+ times.
Admin
You must be a Russian.
Admin
That makes it N * 70+ lowercase calls, where N is the number of opening angular in the page.
Admin
Admin
Or the fiery wheel. Which is all kinds of awesome!
Admin
Wait, does this actually work?
Featured Comment Baby!
Admin
Admin
Admin
Admin
Then again, if you get rid of the quadratic complexity (i.e. converting the whole string to lower-case and possibly anything else that traverses the whole string in each loop), you can shave off a factor on the order of a million for large files, so that's clearly the more important thing here. If that's done and it's still too slow (unlikely), you can care about a measly 70x speedup next.
Admin
Plan ahead. Maybe you should include your list of tags expressed in every possible language, just to be sure.
Admin
It's worse than that, he's dead, Jim.
Admin
Right out of college I worked for a giant global consulting firm with a one-word name that sounds like a sneeze. I wrote crap-tons of J2EE for lots of huge enterprise applications. At that firm, we would have been given bad marks on our review if we had implemented either of the solutions you suggest.
Speed and efficiency weren't really what our project leads cared about; making the code maintainable by cheap commodity programmers later was more their concern. If performance testing showed that the application had a bottleneck, they would just tell the client they're going to need some more infrastructure to drive the finished product.
More than once I brought a module to my lead for a code review, and in the module I had done fairly simple things, like caching the results of expensive methods, or adding a subclass so I could pass data around in logical, sensical ways, and I would be told that it was "too complicated" for future developers to understand, and would I please just code the simplest and most straightforward procedure that met the (barely coherent) specifications and not spend time thinking about how "best" to do it?
Anyway, my bitterness aside, it's entirely plausible that this code was written this way not because the developer thought it was a good idea, but because management found the good idea to be too complicated for their poor little brains.
Admin
This is TRWTF. A general-purpose processor should not have application-specific instructions implemented in hardware.
Sometimes I wish Intel would let their engineers design the chips, instead of having the marketing department do it. (Pentium 4, I'm looking at you.)
Admin
this
Admin
Thank some entity that my homework is only partially implementing HTTP-protocol... Why can't they have nice strict spec on web... Arbitary white space and no enforcement cases.
Admin
The Taginator -- destroying the web one page lookup at a time.
Admin
No, in this case you have an easy reply to Donald: "It is not optimizing, I am only following DRY!"
Admin
This shows that Donald's advice is still good: If you don't write shtty code, there is probably no need to optimize. And if you wrote shtty code, it won't get better if you try to optimize it. Either way rule one of optimization holds: Don't do it.
Admin
I hope it isn't a bad word. But if it is, I'm safe. As long as I don't know what it means, your bad word won't make me think a bad thought.
However if you've made some kind of error, that other people still understand, then they're still thinking bad thoughts despite your error.
So that couldn't be it.
Still confused.
Admin
Probably just a typo, seems to mean shoddy.