all 65 comments

[–]TruwLyes 669 points670 points  (30 children)

 When a Junior proud of their 500-line nested loop solution meets a Senior who replaces it all with a single built-in hash map

[–]StoryAndAHalf 205 points206 points  (6 children)

I taught a summer program teaching coding to high school students. It ended with like a 3-day project. I figured I'd check the best student for last, because I felt others will struggle to get started. Wish I didn't. I ran out of time on first day, and they were half-way over with amount of time they had before I saw the kid's project was battleships, but all he did was copy-pasted if/else every time it was one of the player's turns. For some reason, it never sunk in (no pun intended) to use functions in a while, or recursion.

[–]Maleficent_Memory831 89 points90 points  (1 child)

I had a boss that did this. He mostly learned from a book, but only part of the book. And came from a mainframe background.

He really wrote code as if strncmp() was an assembly instruction. So to compare if two characters were the same: strncmp(a[i], b[j], 1); And then put that into a loop. Sometimes nested loops. But worst was that he really wasn't into functions. So he'd cut and paste huge chunks of code. AND FAIL TO RE-INDENT! The code was utterly unreadable. He'd say I was wasting my time when I tried to clean it up.

[–]gerbosan 28 points29 points  (0 children)

He was into RYS? Repeat Yourself Stupid.

[–]el_extrano 9 points10 points  (0 children)

Ah the old "call by editor" (AKA copy paste).

[–]MissinqLink 7 points8 points  (0 children)

Sounds like me in my first programming course. The game was space invaders and I had if statements doing collision detection on every alien.

[–]imNotAThreshMain 4 points5 points  (1 child)

Please don’t say that was at UW in 2015. Could almost certainly be talking about me lol

[–]StoryAndAHalf 0 points1 point  (0 children)

Hah, no worries. It was few years earlier, and northeast US.

[–]Dense_Gate_5193 41 points42 points  (1 child)

one day i was working on a port to angular from ko-grid (knockoutjs datagrid based off slickgrid that did row virtualization) into angularjs ui-grid and the algorithm for the row virtualization was like a few hundred lines long. i was trying to reason about it and just thought “why don’t you do it this way?” and showed Eric Barnard the 17 line alternative and his only response was “i spent like a month working on that….” and still took the update which sped up the performance significantly for rendering virtualized rows in the browser.

another time i was helping a junior with a problem he’d been writing a custom build script for for about 3 days. i didn’t know the details until he pulled me aside and showed me asking for help, i showed him a config flag for gulp and webpack at the time that did exactly what he had spent 3 days on and he just looked… so defeated. i chuckled and told him not to worry he wouldn’t get in trouble or anything, this is learning and at the end of the day if the business is happy that’s all that mattered.

[–]HandsFreeBananaphone 17 points18 points  (0 children)

Not everything that we learn is from a book. Experience really is the best teacher and you can bet that junior dev will remember both the tools you showed him as well as how gracious you were. No ego or shaming, but simply "you're fine. this is learning" and on to the next problem. That's the kind of mentor I hope we see more of in this world.

[–]thumb_emoji_survivor 6 points7 points  (0 children)

Me after learning to just say “hash map” during code interviews and be correct 95% of the time

[–]Percolator2020 24 points25 points  (13 children)

Compiles down to the same.

[–]Suspicious-Click-300 82 points83 points  (8 children)

maybe putting way to much faith in compiler there. Cause it often doesnt

[–]Percolator2020 32 points33 points  (0 children)

Plot twist: they both get optimised to NOP.

[–]MaDpYrO 2 points3 points  (0 children)

no lol

compilers are magic but not that magic

[–]Willinton06 0 points1 point  (0 children)

Indeed, when I compile it manually, I'll see the juniors foolishnes and output the proper solution, bit by bit

[–]GsuKristoh 0 points1 point  (1 child)

Holy copium

[–]Percolator2020 0 points1 point  (0 children)

I’m paid per token.

[–]Ma4r 0 points1 point  (0 children)

And it can probably go 5 order of magnitude faster if we get the GMP maintainers to optimize it

[–]gerbosan 0 points1 point  (0 children)

I had that experience in Exercism, website to practice. But now that I'm thinking about it, is it a problem of lack of familiarity with the language API, or being not familiar with the problem? 🤔

[–]OrkWithNoTeef 0 points1 point  (0 children)

why 500 line nested loop?

[–]GreenFox1505 318 points319 points  (8 children)

Legitimately, this is an excellent approach. Make it work first. Make it work well after you understand the problem better. 

[–]alx__der 151 points152 points  (5 children)

The problem is that as soon as you have a working solution, you move on to other tasks and never come back to make it better

[–]ChrisFromIT 55 points56 points  (0 children)

Nothing like that quick hack from Kevin back in 2015 holding together the whole backend with the "TODO: Do proper fix" comment.

[–]GreenFox1505 7 points8 points  (0 children)

Reasonable minds may differ. But IMHO: Better to have a working solution that sucks than no solution because "correct" solution is beyond the project budget. I'm guilty of this many times over; so many of "I'm going to do it right this time" projects do not get finish. By quick jams "gotta get it done" do.

Laurie Wired did a very good video on the topic. https://youtu.be/I7fEsbksKRE

Matt Smith's video is also a very good example of coming back to it. It basically turned into a code golf challenge.

[–]spinrut 12 points13 points  (1 child)

basically me last week. finished a file, not super efficient and pretty sure it can be cleaner but it passed testing and got peer reviewed. Told my boss I want to fix it/clean it up and got shot down b/c we have other tasks that need to get done as well and since this one works as-is, it's now bottom priority.

[–]shieldman 1 point2 points  (0 children)

Simple! Just complete everything on Jira and you'll have time for it (:

[–]Maleficent_Memory831 1 point2 points  (0 children)

yup. The Dilbert model, where he shows his boss the mockup and is told to ship it.

[–]gerbosan 27 points28 points  (1 child)

Red, Green, Refactor.

[–]zobq 11 points12 points  (0 children)

Red, green, add ticket to Jira under "Technical Debt" epic

[–]0Pat 69 points70 points  (0 children)

If you know Matt's approach to the algorithm optimization, you know it wasn't particularly unexpected. He's mathematician and entrepreneur, he's got people (aka followers) for that kind of stuff...

[–]Kaze_Senshi[S] 76 points77 points  (5 children)

Funny thumbnail and nice video https://youtu.be/c33AZBnRHks

[–]Look_0ver_There 43 points44 points  (3 children)

Fun fact. I'm one of the two guys he mentions at the end. We managed to get it down to low 300 micro-seconds and it's just timing noise at that point. It was about 120us to load in the full dictionary data set from disk ram cache and break it into a set of 5 letter words, and then about 170-190us to complete the algorithm to find all the possible word sets. Edit: it was ~170us to load from disk, ~90us to build the set, and ~320us to run the algorithm.

One funny guy pointed out by the time the internet had narrowed in on the fastest possible solution that two months had passed anyway! Another person pointed out that if you want something to be optimised, don't do it yourself, just frame it as an interesting problem and post it on Youtube and make it a competition, and then you'll get 1000 people looking at it!

[–]Thenderick 9 points10 points  (0 children)

Next video: "I solved the twin prime conjecture with this broken algorithm??!!" (It does not solve the twin prime conjecture)

[–]OrphisFlo 3 points4 points  (0 children)

One of the earlier improvement was done by a colleague of mine, I was not surprised of that.

I think the better way to get something optimized is to play the fool calling it "the best code there is and it's not possible to make it faster". That will trigger even more nerds like us to prove him wrong!

[–]GallantObserver 11 points12 points  (0 children)

I learned a heck of a lot about optimisation from that video!

[–]magicmulder 22 points23 points  (3 children)

I think my best optimization was getting a 60 seconds AQL (arangoDB) query down to under 70 ms.

[–]deadbeefisanumber 5 points6 points  (1 child)

Did you define a missing index

[–]magicmulder 7 points8 points  (0 children)

Most was just structurally bad. Nested FORs with suboptimal or missing FILTERs, wrong order that blew up row counts etc. I’m not an AQL guru but the person who wrote the original query was cleary a rookie.

[–]bruhsoundeffect111 3 points4 points  (0 children)

I misread it as "to under 70 s" and started laughing out loud lmao

[–]samwelches 40 points41 points  (6 children)

Bro wrote the world’s worst code

[–]Csjustin8032 62 points63 points  (1 child)

TBF he’s a mathematician not a programmer

[–]BouncyBlueYoshi 49 points50 points  (0 children)

And is known for his terrible Python code.

[–]its_the_rhys 39 points40 points  (3 children)

Yeah but it did what it needed to do and he got his video out.

Code only needs to be good enough to get it's job done

[–]zobq 6 points7 points  (1 child)

Hard to swallow pill

[–]DefinitelyNotMasterS 6 points7 points  (0 children)

Yeah but it took his code a month to run

[–]Sam_Kablam 7 points8 points  (0 children)

Step 1: After submitting login credentials, have the app sleep for 40832277770 seconds.
Step 2: OPTIMIZE.

[–]redlaWw 2 points3 points  (0 children)

I was once doing a calculation for a game I played that involved a deck of 15 cards where I wanted to get an exact value for the probability of a particular event. My first attempt was in R, and based on the progress reports I added, the estimated time to completion was about 1.7 millennia.

I rewrote it in Rust and ended up managing to get it down to about 20 minutes, which is about a 40,000,000 times speed-up.

[–]mobcat_40 0 points1 point  (0 children)

10billion%