- 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
They could at least have used a binary search algorithm
Admin
But, but ... pre-increment is faster than post-increment!
Admin
I mean, it's Python. Unless there's some extra stuff done with the list, they could just iterate over it.
Admin
If you have a problem with "exceptions for flow control" in Python, you missed a trick. That's how for loops know when an iterator or a generater is done: using a StopIteration exception.
Admin
And this
json.load(html)
is another kind of WTFAdmin
You mean because a variable named html is a file handle?
Admin
Using exceptions for flow control is OK when your catching a specific known exception. This sort of code is the equivalent of basics On Error Resume Next code. If something unrelated goes wrong while in this loop it thinks it has reached the end of tdata and tries to continue while ignoring the actual error. What this code should be doing is catching the out of bounds error and letting other exceptions continue to the general exception handler.
Admin
Honestly the last time I saw code like this was years ago and it was Java code doing the exact same thing. It was turned in by an Intro to Programming student who it turns out had asked online for solutions to some homework problems and some "kind soul" on Yahoo Answers decided to make sure they got caught cheating on their assignment by giving them a loop like this one as part of the solution, exceptions being thrown to terminate the loop and all.
Admin
@Remy: there's a missing word "not" in the Easy Reader version:
<--Easy Reader Version: and yes, this was real code, and [not] a classroom exercise of "find the length of a list without using
len
" or something. -->Admin
I guess that was a code that counted lines on different file format, e.g. CSV or JSONL (possibly even html). You can't call len(test_fhandle). The second guess is coding by stackexchange without working brain.
Admin
It's Shlemiel the Painter's Algorithm in its natural habitat!
( https://www.joelonsoftware.com/2001/12/11/back-to-basics/ )
"Shlemiel gets a job as a street painter, painting the dotted lines down the middle of the road. On the first day he takes a can of paint out to the road and finishes 300 yards of the road. “That’s pretty good!” says his boss, “you’re a fast worker!” and pays him a kopeck.
"The next day Shlemiel only gets 150 yards done. “Well, that’s not nearly as good as yesterday, but you’re still a fast worker. 150 yards is respectable,” and pays him a kopeck.
"The next day Shlemiel paints 30 yards of the road. “Only 30!” shouts his boss. “That’s unacceptable! On the first day you did ten times that much work! What’s going on?”
“I can’t help it,” says Shlemiel. “Every day I get farther and farther away from the paint can!”
Admin
There was a very brief period (ca. Java 1.0, IIRC) where the actually fastest way to loop through an array in Java really was to start up your for loop and exit by catching whatever Java's index-out-of-range exception is called (... probably IndexOutOfRangeException, but I haven't touched Java in a very, very long time). Bounds-checking was expensive, from what I remember, but the details of why escape me.
I do sometimes wornder if the real WTF is companies continuing to try to build things with the sorts of languages that were created to be teaching tools. I understand the mechanisms that cause that sort of thing, but it does seem to create problems like this.
Admin
@Canthros ref
The unholy mess that is our tools and environments is worse. See also https://xkcd.com/2347/ and https://xkcd.com/2166/
Admin
It's because managers don't know any better, tech leads only know the one language they were forced to learn in school and HR thinks that hiring people with the one language you are forced to learn in school is a guarantee for a large pool of candidates who can therefore demand less money.
Admin
Yep. Exceptions should be for EXCEPTIONAL circumstances. use return codes or named, defined exceptions for errors that you can expect as discovered in design and testing.
Admin
Odds that the input looks like this?