• WTFGuy (unregistered)

    The performance on his test data which has only a frist record was just fine.

  • Prime Mover (unregistered)

    ... don't tell me:TRWTF is that he actually wanted it to return the earliest element in the list, and he's still puzzling over why it's returning him the latest, even though he (I'm presuming "he") has put in all sorts of stuff to force it to do what he wants ... "hmm ... obviously the compiler's broken ... let's introduce a variable i and make it equal to 1, and force the compiler to do what I want it to ... nope, still broken ... I know what, I'll chop the list to pieces upstream and delete everything except the earliest one ..."

  • Tim (unregistered)

    How they managed to do something as advanced as this without even using a temporary table and a shell command is a work of genius.

  • van Dartel (unregistered)

    Given that LINQ is already willing to do a lot of optimizations now, it should be willing to go further and also consider the ToList() and its subsequent loop in its compile-time analysis. Some kind of AI-driven compiler seems to be the perfect solution to deal with NI-driven programmers.

  • WTFGuy (unregistered)

    It may simply be a version of the ever popular speed up loop: https://thedailywtf.com/articles/The-Speedup-Loop

    Far more likely, as I said IIRC just yesterday, this is a dev who learned about arrays, for loops, and strings. Everything else about programming and his chosen language is a mystery he can only cut-and-paste, never understand. To him LINQ is pure voodoo. As is fluent coding style.

  • Dude (unregistered) in reply to WTFGuy

    To be fair, Linq is pure voodoo, and I'm saying that as someone who uses it frequently and even understands a fair bit of what it's doing behind the scenes.

  • my name is missing (unregistered)

    To read this comment you must read all the comments ever in TDWTF.

    But really, if you have a SQL database, make it send you what you want, and nothing more or less. Yet I have seen this pattern forever. I once worked at a healthcare company, and we wanted our users to be able to search for a claim at one of our insurance company partners, when I asked about a search they told me "we only provide 10 at a time listing with random order; your users are welcome to page through to find their claim". For thousands of claims.

  • Dlareg (unregistered) in reply to my name is missing

    One of the things I always keep in mind (and in the minds of my team). Never make your problem your users problem. They tend to pay the bill.

  • The Dave G (unregistered)

    It's too early in the day for something this bad. Better to see it in the evening, where it can give you nightmares all night.

  • Sole Purpose Of Visit (unregistered) in reply to van Dartel

    To be fair to the compiler, Linq is extrinsic (as is any other library). There's no way to optimise this, short of shooting the programmer and starting over.

    And to be fair to the programmer, this here is a magnificent piece of Planning For The Future. It will definitely come up with the right answer, assuming more than one matching row in the db. You can't say that for a Linq call, can you? I mean, a future programmer might update the using-clause to something like "using Bozo". Or perhaps Microsoft will take Thor's Hammer to backward compatibility and utterly change the semantics of the call.

    And the pattern is repeated all over the code base? Consistency, man, consistency. It's the only way to guarantee that any bug you find will be exactly the same bug, all over the place.

    Minor nitpick to Remy: "FirstOrDefault" is called that for a reason. It isn't "FirstOrNull." And the distinction is important when writing .NET code.

  • Jonathan (unregistered) in reply to WTFGuy

    It also seems to me that the developer apparently doesn't actually know how to code.

    The first line could be forgiven as a rookie mistake, in my first couple of months as a developer I made the same one (was learning a new language at the same time too), but once I understood that .ToList() triggers a DB query, I never made the mistake again. In fact, I now consider myself highly knowledgeable about C# ORMs, their strengths, weaknesses and gotchas.

    But when you look at following lines, it's a total WTF, clearly the "coder" has not a clue of what they're doing.

    If I were to guess based on some people I've worked with, they try to get by by pasting code from SO, continuously tweaking the code until F5 "magically" works and then repeating those steps until running the program seems to produce the desired behaviour. All the time without actually understanding how anything works. They figuratively bash it with a hammer until it "goes in".

    I have a more specific example. A Junior (being mentored by someone else) had been stuck for a couple of days on a relatively simple task of making a pretty basic AngularJS filter, so I thought I would have a try at helping him work through the problem.

    I saw their task as two problems, one of them was understanding the syntax and structure of AngularJS filters. The other was about the very basic task of filtering a list or formatting or something in plain vanilla JavaScript. So I told him to forget AngularJS and the entire SPA for the moment and just focus and the very basic JavaScript algorithm that would be needed.

    I advised he just use Chrome's Developer Tool's console to quickly create the basic function which given an input returns the correct output and I showed him a very basic example of writing a basic function in the developer console, then let him try. He could not even manage that, he literally struggled at the most basic coding literacy.

    At which point I had a chat with his manager and said we have a big problem.

  • akozakie (unregistered) in reply to Sole Purpose Of Visit

    There's no way to optimise this, short of shooting the programmer and starting over.

    Doesn't look like a bad optimization approach in this case, does it?

  • Jaloopa (registered) (unregistered)

    This isn't a WTF. FirstOrDefault won't tell you if you've received no results or if the first result is null. This way, if there's no match then the order isn't printed

  • (author) in reply to Sole Purpose Of Visit

    True, but the default in this situation is almost certainly null.

  • Scott (unregistered)

    Reminds me of the idiot consultants I used to work with. Aggressively ignorant, "linq and EF are too hard". So rather than ask questions and learn how to use these tools better, they'd do stuff like above.

    Like Johnathan's example, one of these idiots once couldn't figure out some simple js. Allegedly a very experienced web developer, I suggested using dev tools to look for errors. "What's that?"

  • Naomi (unregistered) in reply to Jonathan

    That reminds me of my first job (well, technically an internship, but for all practical purposes I was a junior developer without the salary). With a few weeks to go before I went back to school, they were looking to hire a junior developer, and since the position would involve taking over my project, they asked for my opinion on one candidate. My opinion was that he couldn't program his way out of a wet paper bag and told them so. Needless to say, I was ignored, he was hired, and he couldn't program his way out of a wet paper bag.

    The guy said he was really into TDD, so I asked him to write a simple unit test - I'm talking really simple, thing.frobnicate(); assertTrue(thing.isFrobnicated());, that I'd just never gotten around to (my younger self had a much more blasé attitude about such things). Guy's like, "can do!" and I go back to my own work. Well, a few hours later, he comes back to ask for help.

    He had dozens of lines attempting to construct Mockito mocks around standard library classes (not only Lists, but Strings as well, wtf?). Nowhere in any of it was anything to do with our code base.

  • Sole Purpose Of Visit (unregistered) in reply to Remy Porter

    "Almost certainly" is the enemy of "correct."

    Since we don't know anything about the Order class, it's very possible that there's a default constructor in there.

    Then again, you're right. It boggles the mind that somebody has "learned" how to use Linq and yet hasn't even tried to find the relevant method via the handy VS auto-complete (plus XML doc) tool. No matter how hard you try to defend against idiocy, there's always a better idiot out there, isn't there?

    (My personal favourite around these parts is a cow-orker who uses for(;;) on a Linq result, with the index going backwards from Count(). I had to explain to him that there's actually a nifty method called "Reverse" that ... does exactly that.)

  • (nodebb) in reply to Prime Mover

    (I'm presuming "he")

    You know what happens when you presume things. You make a prez out of U and me.

  • WTFGuy (unregistered)

    This dev is obviously as idiot as I noted above. But, as my name is missing almost said, this flows from their ORM mapper not having a method that does what the dev needs. Heck, there may not even be an ORM mapper; just a pile of ad hoc DB access code as bad as what we do see.

    In any case devs who do end user UI work should not be noodling through result sets to find the subset they need: they should be creating requirements for the middle tier devs to deliver the subset they need.

    The truth is none of us can say what horrors lurk inside db.GetOrdersByDateDescending(). So we also can't say how well LINQ or even a magic clairvoyant compiler can/could do something smart with it.

  • Felix (unregistered) in reply to Sole Purpose Of Visit

    The default is the type default, so for objects that is always null. You would have to return something like an innr or struct to get a non null default object.

    So querying the database for the first element and checking for a null response will always work as expected for object results. For all other you could cast the result to int? Etc before the FirstOrDefault call to get a nullable result to check

  • Sole Purpose Of Visit (unregistered)
    var pointless = orderList.OrderByDescending().Frist();
    var equallyPointless = orderList.OrderByDescending().Lsat();
    if (pointless.ApproxEqual(equalityPointless)
        Console.WriteLine("Frist is Lsat and evermore shall be so!");
        throw new Exception("You might want to think a little harder before you commit this to production.   Just, y'know ... God Is Watching You.");
  • (nodebb)

    They could at least have exited the loop early on a match!

  • van Dartel (unregistered) in reply to Sole Purpose Of Visit

    A language/api can be designed to reduce the desire/need for optimization (obviously extrinsic, at least at compile-time). For example, instead of getAll().first(), there could be a getFirst() (without a care for programmers who didn't read the manual). But besides that minor point I agree with everything you said ;-) PS: perhaps I should have clarified that NI does not stand for "Natural Intelligince" but for "Nominal Intelligence".

  • (nodebb) in reply to Tim

    Temporary table? Why not just print out the list, put the printout on a wooden table, take a photo of it, fax it to yourself, scan it in, and OCR it?

  • David Mårtensson (unregistered) in reply to van Dartel

    No, ToList is explicitly intended to create a list.

    No level of analysis can ensure a lazy load to be safe.

    You could have a situation where there are triggers in the DB that need to be triggered on read (which would fit this kind of code :P )

Leave a comment on “Frist Item”

Log In or post as a guest

Replying to comment #:

« Return to Article