• huppenzuppen (unregistered)

    They could at least have used a binary search algorithm

  • Sole Purpose Of Visit (unregistered)

    But, but ... pre-increment is faster than post-increment!

  • Birion (unregistered)

    I mean, it's Python. Unless there's some extra stuff done with the list, they could just iterate over it.

    for item in tdata:
    
  • (nodebb)

    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.

  • (nodebb)

    And this json.load(html) is another kind of WTF

  • (nodebb) in reply to tom103

    You mean because a variable named html is a file handle?

  • JM (unregistered) in reply to Uplink

    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.

  • (nodebb)

    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.

  • (nodebb)

    @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. -->

  • sokobahn (unregistered)

    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.

  • Deeseearr (unregistered)

    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!”

  • Canthros (unregistered)

    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.

  • WTFGuy (unregistered)

    @Canthros ref

    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.

    The unholy mess that is our tools and environments is worse. See also https://xkcd.com/2347/ and https://xkcd.com/2166/

  • (nodebb)
    Comment held for moderation.
  • LZ79LRU (unregistered) in reply to Canthros

    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.

  • Matthew Persico (unregistered) in reply to JM

    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.

  • experienced enterprise dev (unregistered)
    {0:"hello",1:"world"}
    

    Odds that the input looks like this?

Leave a comment on “Going to Great Len(gths)”

Log In or post as a guest

Replying to comment #:

« Return to Article