- 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
public static string GetFrist(int id) { for (int i = 0; i < GetProductCodes().size(); i++){ // do nothing :) } return "frist!"; }
Admin
You know you have a WTF when your utility functions behave like disk benchmarks.
Admin
The precise grade of WTF depends on what
GetProductCodes().get(i)
does if there isn't a product with that ID.Addendum 2018-10-17 07:16: EDIT: Not to mention the disconnect between "get a product with this ID" and "get the product at position
ID
in the list"...Admin
This is an example ofa typical code duplication we found en masse in our legacy code. Some programmers argue that it's faster to write and easier to read this way instead of using an additonal line "List productCodes = GetProductCodes()" above the loop (and then referring to that variable in the loop) . And they argue that it is not a performance issue because the compiler could optimize it anyway. They are very astonished when I tell them that the compiler is not allowed to optimize here and that the method it is not an easy getter but could do performance-intensive operations (like access a database) or being changed later to do so. And that GetProductCodes.size() is executed every time in the loop, and if someone changes the size (in a parallel thread) while still iterating, the loop could crash or skip an item! I already had this discussion with many developers, so it is a real WTF that exposes the habit of a quick-and-dirty developer or beginner. I suspect that the IDE-autocomplete somehow supports this kind.
Admin
I have produced some really stupid code in my life, but this is the single most awe-inspiring example of programming idiocy I've ever seen. Even if you have no idea what GetXYZ() does, even if you have no concept of data(base) handling, even if some external condition or legacy code forces your hand to produce more bad code - a somewhat talented, learning or at least responsible-minded programmer just HAS to feel it in his guts that these three lines are the devil's work.
Admin
There is certainly a WTF here. It's definitely bad code.
But it's still not quite reasonable to expect a new team member to know calling GetProductCodes has the side-effect of making the hard drive chatter away. Even if that person in Mads Kristensen himself.
Javadoc or XMLdoc comments and a good IDE would help that person.
///
Admin
It calls the method ID * 2 times, once for size and once more for get.
Admin
I am thinking that the refactored code would be: (I think this was originally in Java, but I wrote this from a C++ perspective, as I am much more familiar with C++)
Also, look at where this function is called. It might be more efficient to inline this code, and only call GetProductCodes once, especially if this function is called in a loop and you do nothing with the null return value.
Admin
This is a beautiful example of Enterprise level code. Carry on the good work!
Admin
Nope. The call to get is conditional and only done on the one that matches, so the number of calls is
id+1
.Addendum 2018-10-17 08:47: Argh, and I'm wrong, too.
id+2
Admin
The big O of this function is OMG.
Admin
If this "ended up in source control" amongst a team full of people who recognized the subject is a pretender, then clearly the place's code review standards are worse than its hiring ones.
Admin
How do you review code that is not in source control?
Admin
...and his next project yeilded a Bubble sort using GetProductCode(int).
Admin
It could be a map that maps id -> productcode...
In this case there is a bonus WTF: the code will stop working once lower ids get deleted, making some ids higher than the map size.
Admin
Pre-commit reviews are a thing. You could use Review Board or Crucible, or you could send your patch to a mailing list.
Admin
Blue screen joke. That was real wtf?
Admin
Meh, I've seen worse. One programmer was tasked with writing a procedure to read the Nth line of a text file.
The delivered something, and indeed it read the first through fifth text lines just. fine. The sixth line took two seconds to show up. The seventh took 20 seconds. We didn't wait long enough for the seventh to show up. A peek at the co\de showed why. pseudocode:
proc readxththline(x): rewind(file)
proc readline() ch = '?' y = '' while ch <> '\n': readch(file, c) y += c
return y
well, actually, I think there was another rabbithole, where to read the n-th character it would rewind and read n characters too. So line O() a fourth power. Amazing code.
Admin
Well, it could've been worse. The so-called developer could just pick up random products until he got the one he wanted.
Admin
I read the article as implying "this appeared and now they have to deal with it". I suppose it could have meant "this arrived at the gate and it was angrily turned down".
Or "this was pushed through review because Big Boss knew it had to be good since [candidate] wrote it". But if that had happened I'd assume it would have been part of the article.
Admin
This isn't Rails, but I see it a lot with Rails programmers - they just have no concern for performance at all and seem confused by the concept that code which runs without faulting and produces the right output can still be bad. I've rewritten their 10 hour overnight processes to run in less than a minute. This 'read the file N times' is a very common anti-design pattern, for instance, and trivial for me to optimize out. Or iterating through a gigantic sorted array without using a binary search, or iterating through a hashset/dictionary, etc. etc. 'Oh, but it's more clear that way'. Only if you're brilant! And I'm sure there are some performant RoR programmers out there, I just haven't met any.
Admin
"How do you review code that is not in source control?"
With pre-commit patches or a veritable plethora of other methods.
Admin
"Pre-commit patches"? So the programmer is supposed to get a copy of the source from the branch being worked on, run diff(1) on it and the modified copy, email everyone else with the results and if they're all happy with what they see the modifications get to be saved into the VCS? There must be a more robust method than that - there's no guarantee that what gets saved is what was used in the diff. I don't like that one. What are the other methods? I thought of everyone taking turns to shoulder-surf everyone else to see the code before it gets saved, but there's still no guarantee that what gets committed is what was reviewed.
Admin
You really think adding a "List productCodes = GetProductCodes()" would decrease the WTFness of that code? Stop giving advice to other people right now, you're not qualified!
Admin
Re: Pre-commit patches: You commit the change to the in-progress branch, which is what most people are working on. That kicks off a code review. If everyone like what they see, then that patch gets merged into the production branch. If not it gets reverted or fixed.
If you're really fancy, like JIRA, you can do the code review entirely online with no icky human contact required. It shows relevant people the patch and they can comment on it and say 'fix this first' or sign off on it. If everyone signs off, closed, promote, done. Of course if anyone wants they can request a meatbag facehole discussion on it, but that's rare.
Admin
In what f***ed up mind is iterating through a dictionary to find an item with the right key "more readable" than just indexing with the key?
Admin
That's essentially how processes like Phabricator/Maniphest work, although (a) the review itself is webby, and the email just says where to find it. There is then a fussy distinction about whether "not in source control" means "not in source control anywhere" or "in my local git repo but not in the central 'master' repo". Phabricator works best on the second rather than the first, obviously.
And anyone who puts one thing up for code review and then (deliberately) commits something different should be chastised severely. I recommend strapping him to a wall "downstream" from the loud end of a GAU-8. (Yes, I am advocating murdering him for this level of malfeasance. It's not a totally serious suggestion.)
Admin
The sort that, when presented with a need to "search the dictionary for the item", writes code to do just that, rather than asking the dictionary to search itself. That way it's clear to any reader that the code is searching the dictionary... (Fix: change the required action to "get the item from the dictionary" rather than "search the dictionary for the item" or "find the item in the dictionary".)
Admin
I had an off-site/out-sourced/overseas contractor (I am a contractor too) that I had to call up at 4 AM in the morning (my time) to coordinate with.
This contactor did something similar with an iteration over a list, except that each iteration called a SOAP service which in turn did a query on a database. Suffice to say, it was very slow. I should not have had to explain to the dev that this was not desirable (much less not optimum) but I did.
Admin
"...and if it's the last product code in the datafile, you have read the file N times."
N: undefined variable
Admin
The big question is: how was the new hire related to the boss? Or what kind of “services” did the n00b perform for the boss.
Admin
"Pre-commit patches: You commit the change to the in-progress branch,"
That does not sound like a pre-commit patch...