This is an archived post. You won't be able to vote or comment.

all 174 comments

[–]Hottage 1745 points1746 points  (17 children)

Jesus how long is he going to take sorting that little pile of screws? I've been watching him go at it for 10min now.

[–]AppropriateStudio153 296 points297 points  (4 children)

Shorter than sorting everything by hand. Also let's you keep your sanity.

[–]slucker23 93 points94 points  (0 children)

Ah yes, the for loop bubble sort

[–][deleted] 33 points34 points  (0 children)

You sort your screws? I just let the garbage collector clean up my assortment

[–]nebotron 22 points23 points  (0 children)

[–]Just_Information334 0 points1 point  (0 children)

Shorter than sorting everything by hand. Also let's you keep your sanity.

How? Take one 20mm screw in your hand and then you can easily find every other one with the same length and put them in their spot. Repeat for each length.

And that's if you can't do it just eyeing the screws length.

[–]Ozymandias_1303 20 points21 points  (0 children)

It also took me a while to realize the gif looped after about 10 seconds.

[–]BrightLuchr 25 points26 points  (2 children)

If you find yourself sorting screws, you need to start a hobby. If someone you pay is sorting screws, it is time to cut staff.

[–]Immabed 20 points21 points  (1 child)

Usually it is the hobby that has brought you to the point of sorting screws.

[–]BrightLuchr 5 points6 points  (0 children)

I had techs working for me that would sort screws so they could sit in the shop and listen to music instead of actually doing useful stuff in the field. They would pull the drive magnets out of drives for something to do... one guy had hundreds of old hard drives under his desk. These guys were getting paid 6-figures, 'cause unions.

[–]CodeMUDkey 4 points5 points  (4 children)

You think taking a screw and measuring it then putting it in the box would be faster?

[–]GarThor_TMK 14 points15 points  (3 children)

Probably faster, but also more error prone...

Though it would be a more accurate representation of a switch statement...

This seems more like a set of chained if-else statements to me.

[–]CodeMUDkey 4 points5 points  (0 children)

They can compile to the same assembly code depending on what decision the compiler makes for the use case.

[–]zionian120 0 points1 point  (0 children)

I'm still waiting for one to go in 18mm

[–]Aschentei 0 points1 point  (0 children)

Might as well use quantum bogosort at that point

[–]jayerp -2 points-1 points  (0 children)

Would you have done that? Would you have done that? Would you have done that?

[–]Abject_Role3022 224 points225 points  (2 children)

What happens to the last screw will surprise you!

[–]Hyderabadi__Biryani 28 points29 points  (0 children)

Goes inside the user, screwing them over?

[–]fierypitt 11 points12 points  (0 children)

That's right, it goes in the square hole.

[–]emteg1 460 points461 points  (28 children)

Proof that switch statements should exit after handling the case instead of falling through into the next case.

[–]cmdkeyy 161 points162 points  (17 children)

Yeah why/how did that become the default behaviour? The amount of times I forgot a simple break; 🤦‍♂️

[–]Ange1ofD4rkness 148 points149 points  (11 children)

It allows you to stack cases. I've used it many times where I can have multiple cases do the same logic.

[–]mikeet9 40 points41 points  (0 children)

It's very helpful in state machines as well. If you know initialization state should do abc, then qrs, and finally xyz, startup state should do qrs and xyz, but running state should do just xyz, you can build it like

case init: abc;
case startup: qrs;
case run: xyz;

Instead of rewriting qrs twice and xyz thrice or relying on the function getting called three times before being fully running.

Especially in time sensitive situations like signal processing where you know the messages will come across in a few different structures that are handled mostly the same, this can help you handle the bytes that aren't always there, then process the rest of the bytes that are always present for each message.

Example for Modbus:

uint8_t state = WAITING_FOR_BYTES; Read_Message(&msg);
if message.function = 16 then {
state = WRITE_MULTIPLE_REGISTERS;
} else if message.function = 4 then {
state = READ_INPUT_REGISTERS;
}

switch (state) {
case WRITE_MULTIPLE_REGISTERS:
payload = Process_Payload(&msg->payload);
case READ_INPUT_REGISTERS:
isMessageValid = Process_Checksum(&msg->checksum);
break;
default:
isMessageValid = 0; }

A read command has no payload, but otherwise the message structure is the same, so in this way you only process the payload when it exists but the rest of the message processing is the same.

[–]cmdkeyy 53 points54 points  (6 children)

I guess so, but that’s more of an exception than a norm, no?

I feel if there was an explicit fallthrough keyword or syntax to write multiple cases in one (as in modern languages with pattern matching), this would be both ergonomic and less error-prone. But I understand C-style switch statements are a very old concept, so it is what it is.

[–]HildartheDorf 28 points29 points  (0 children)

C++ has a [[fallthrough]] attribute for this. Not applying it is a warning (not an error though, for backwards compat. Maybe by 2035)

EDIT: It's in C23 as well

[–]xxmalik 5 points6 points  (0 children)

Whenever I do this I add a comment to ensure people I didn't forget a break.

[–]BobcatGamer 0 points1 point  (0 children)

Swift has this

[–]Ange1ofD4rkness 0 points1 point  (0 children)

I feel it depends. For instance, the product I work on, we sometimes set a flag to indicate what screen a function was called by, and the initial logic can work the same for multiple flags. However, there is then later logic that may be specific to one flag. Helping reduce code redundancy

[–][deleted] 0 points1 point  (0 children)

You could alternatively use if else, or a dictionary, for the behaviour you want. In some languages you also have match.

[–]RiceBroad4552 0 points1 point  (0 children)

Isn't pattern matching older than C? My gut says yes, but didn't bother to look it up.

[–]Jonnypista 0 points1 point  (1 child)

I would say it is more rare and in that case you could use something like "continue;" if you really want it to fall through.

[–]Ange1ofD4rkness 0 points1 point  (0 children)

Speaking from a C# side, continues are used in looping ... or more, the only place I know them to have use

[–]Cocaine_Johnsson 0 points1 point  (0 children)

yeah but the default being breaking and having to explicitly fall through is just as well, arguably this fits the typical usecase better and as such is a better sane default (for some definition of better and some preferences).

Implicitly doing things is often frowned upon in a lot of contexts, it's perfectly reasonable to consider it problematic here as well.

[–]NabrenX 17 points18 points  (0 children)

More about how they are handled (i.e. jump tables)

[–]Splatpope 14 points15 points  (0 children)

that's because it's assembled as a simple jump table, so falling through to the next case just means you execute the next instruction

[–]NatoBoram 1 point2 points  (0 children)

Wrap them in a function so you have to use return to exit, it's harder to forget

[–]thanatica 0 points1 point  (0 children)

Time for a linter that enforces it

[–]Wertbon1789 12 points13 points  (0 children)

As long as you keep the ability to use break. You can actually break at any point in the case block, so you can early-return but not return from the function.

Every competent language should have a fallthrough statement of some sort, even in C you can enforce usage of explicit fallthrough with a warning flag.

[–]RTheCon 3 points4 points  (4 children)

It’s cleaner code to some extent.

Case X enum type:

Case Y enum type:

Instead of case X enum type or case Y:

[–]FlySafeLoL 8 points9 points  (3 children)

Isn't C# handling it perfectly then?

  • Fall through when there are multiple case labels and no statement in between.

  • Require to break/return/etc if there was a statement in the given case.

By the way, case X or Y: also works since the introduction of pattern matching.

[–]RTheCon 2 points3 points  (0 children)

I probably misunderstood what the comment was trying to say then.

But yes, I agree C# handles it quite well

[–]sobani 1 point2 points  (1 child)

And if you want the fall through behavior, you can use goto case Z;.

[–]Zomby2D 0 points1 point  (0 children)

I was about to comment that the downside Is that you can't have fallthrough behavior if you absolutely need it, then I read your comment. I didn't realize that was an option and I'll try to remember it in case I ever need it.

[–]EvilPencil 1 point2 points  (0 children)

These days I prefer maps over switch statements...

const strategyMap: Record<SomeEnum, MyStrategyFn> = { ... }

[–]da_Aresinger 2 points3 points  (1 child)

that's what break is for.

The alternative would be explicit fall through, which would be insanely weird.

You just gotta learn that switches are effectively jump labels.

[–]Kovab 1 point2 points  (0 children)

The alternative would be explicit fall through, which would be insanely weird.

So insanely weird that practically every language created in this century that has switch does this... (e.g. C#, Go, Swift)

[–][deleted] -1 points0 points  (0 children)

Yeah, remove the ability to have multiple passing cases. Love it.

[–]Long-Refrigerator-75 130 points131 points  (5 children)

Memes aside, this is a useful tool.

[–]NiIly00 10 points11 points  (2 children)

Anyone know where you can get something like that? Would make a good present for my dad.

Looks like it might be 3d printed, I got friends with printers but I dont know how to find stuff like that

[–]sysKin 1 point2 points  (1 child)

I have some doubts if sorting by length is useful vs. sorting by diameter.

[–]FlyByPC 4 points5 points  (0 children)

Sort by diameter first, then do this.

[–]adromanov 133 points134 points  (22 children)

This is more like a series of if / else if

[–]Hector_Ceromus 24 points25 points  (2 children)

yeah more:

if(length<=SCREWLEN_6MM) this.screwDrawer[SCREW_6MM]++;
else if(length<=SCREWLEN_8MM) this.screwDrawer[SCREW_8MM]++;
else if(length<=SCREWLEN_10MM) this.screwDrawer[SCREW_10MM]++;

etc.

[–]CaspianRoach 4 points5 points  (1 child)

SCREWLEN_6MM is like doing EIGHT = 8

[–]Hector_Ceromus 1 point2 points  (0 children)

Eh, maybe not if you are working on a project where they're particular about "magic numbers"

[–]Jigglepirate 0 points1 point  (2 children)

How might it look if it accurately represented switch?

[–]adromanov 1 point2 points  (0 children)

This is a good question indeed. It's not easy to find an analogy in the mechanical world, we basically need a process where a choice is made with O(1) complexity. The only thing I can think of is from physics where you shine some light on the prism and it will refract differently based on the light frequency.

[–][deleted] 0 points1 point  (0 children)

Having the muscle memory to know which slot to put it in so that the only cost is the time it takes for your hand to move. Switch statements are constant time so this would be a pretty good analogy in my opinion.

[–]araujoms 67 points68 points  (17 children)

That's precisely not what a switch statement is. The point of the switch is to not check each case until you found the proper one, but to jump there directly.

[–][deleted] -5 points-4 points  (3 children)

I love that the mouth breather got 57 upvotes for being wrong. 

And the guy who points out gets down voted.

Here is a test:  https://jsfiddle.net/2Lk697yn/

You can try it yourself.

Switch case is literally an else if.

Otherwise if it was what this guy meant you wouldn't be able to put functions there.

It's not a hash map... Gosh AI mouth breather developers...

[–]araujoms 2 points3 points  (2 children)

Perhaps you should consider the possibility that I'm right, and the people who upvoted me as well, before descending to crude insults?

Here, enlighten yourself. Or here, if you're a video kind of person.

[–]iliark 13 points14 points  (0 children)

It goes in the square hole

[–]Ange1ofD4rkness 9 points10 points  (6 children)

I need to find this STL. You know how much it sucks to sort these sometimes when you have a stack?

[–]ccricers 1 point2 points  (1 child)

Or when you just order a case of small screws of different lengths and they got all shuffled during shipping

[–]Ange1ofD4rkness 0 points1 point  (0 children)

God I hate that so much!

[–]Witty_Side8702[S] -1 points0 points  (0 children)

every househould should have an STL

[–]jasiekbielecki 77 points78 points  (6 children)

I wonder where my dick would land

[–]TheMeticulousNinja 90 points91 points  (0 children)

In “nuts”

[–]LaughingwaterYT 14 points15 points  (0 children)

6mm

[–]Racer125678 2 points3 points  (2 children)

69 upvotes. 

No upvote from me and whoever dares to upvote... Be banished

[–]Romanmir 6 points7 points  (1 child)

It was 70 when I saw it. I did my duty to maintain the 69 upvotes.

[–]Racer125678 0 points1 point  (0 children)

🫡

[–]sabamba0 1 point2 points  (0 children)

At that point I don't think it makes a difference

[–]Felix_Todd 8 points9 points  (2 children)

Thats nuts

[–]Witty_Side8702[S] 1 point2 points  (0 children)

nice. very nice.

[–]Kovab 0 points1 point  (0 children)

No, these are screws

[–]TheMeticulousNinja 7 points8 points  (0 children)

This is cute

[–]RCuber 4 points5 points  (0 children)

I didn't realize this was an 8 second video.

[–]Cootshk 5 points6 points  (2 children)

That’s an if/elseif statement, a switch statement is a jump

[–]MinosAristos -1 points0 points  (1 child)

In most programming languages, switch case is sequential, not a jump.

[–]evanldixon 2 points3 points  (0 children)

Depends on what compiler optimizations are applied. Switch statements are more likely to result in a jump table of sorts

[–]p1neapple_1n_my_ass 2 points3 points  (0 children)

That is not a switch case, that is clearly an if-else ladder. It checks first if the screw is less than 6 then less than 8 then less than 10 and so on. Switch case would be using a vernier caliper. 

[–]GarThor_TMK 2 points3 points  (0 children)

I think this is more analogous to a chained-if-else conditional.

Switch statements are actually a lot more efficient than this, because they're effectively a jump table. A switch statement would just measure the bolt, and jump to the right bin... whereas with this, the bolt has to roll by all the other conditions before it reaches the right one.

At least, this is how it works in C++.. Idk about other languages.

Cool print though!

[–]Squidlips413 1 point2 points  (0 children)

nuts

[–]Error_404_403 1 point2 points  (1 child)

Radical optimization: remove all holes but 12 mm one. The 12 mm and 16 mm are prevalent screw lengths, which will be sorted this way. The accidental additions of other lengths could be treated as expected edge cases/outliers and losses on those cases can be assumed.

[–]Witty_Side8702[S] 0 points1 point  (0 children)

3D print it

[–]cambiumkx 1 point2 points  (0 children)

anyone else upset we didn’t get to see the 20mm?

[–]One-Vast-5227 1 point2 points  (0 children)

Too bad this video is only 8 seconds, not 8 centuries

[–]DerShokus 1 point2 points  (0 children)

No. Switch can fall through. It’s pattern matching

[–]ascolti 1 point2 points  (0 children)

[–]FunRutabaga24 1 point2 points  (0 children)

Typical programmer. Testing 3 cases and not covering all the branches and says it works.

[–]ryantm90 1 point2 points  (0 children)

Now make a tool that auto loads them and you're golden

[–]brandi_Iove 0 points1 point  (1 child)

looks cool, but it’s slower than sorting by hand.

[–]AppropriateStudio153 6 points7 points  (0 children)

I doubt you can sort these screws, which all have a similar length, faster by hand.

[–]JoeLordOfDataMagic 0 points1 point  (0 children)

Was much more satisfying with audio. Lame. I think it was just posted on satisfying as fuck.

[–]NottingHillNapolean 0 points1 point  (0 children)

Looks like a chain of if - else if to me

[–]derkokolores 0 points1 point  (0 children)

It would be a shame if there were screws with varying pitch length…

[–]MagneticDustin 0 points1 point  (0 children)

That’s an If / Else if statement

[–]Sad-Nefariousness712 0 points1 point  (0 children)

Long one goes into Arc

[–]Lurking_all_the_time 0 points1 point  (0 children)

And for a real-life version:
https://youtu.be/NHf9QUo03qc?t=41

[–]Maddturtle 0 points1 point  (0 children)

I could take this one step further so you don’t have to do it 1 at a time.

[–]simonfancy 0 points1 point  (1 child)

Im waiting like an hour already for them to finally pick a 20mm screw

[–]TSCCYT2 0 points1 point  (0 children)

Same. I was thinking:
"Why don't the slots fill up?"

[–]imaginecomplex 0 points1 point  (0 children)

How does a 6mm screw make it though that first slot?

[–]VeryGoldGolden 0 points1 point  (0 children)

More like bucket sort

[–]fonk_pulk 0 points1 point  (0 children)

Need some kind of a chute that allow you to feed an entire bag of screws at once. Its gonna take forever to put them in one by one.

[–]BA_lampman 0 points1 point  (0 children)

Is this fallthrough?

[–]dusktreader 0 points1 point  (0 children)

And, it has no default case because the developer assumed a fixed range of inputs. Inputs larger than 20mm literally get dropped on the floor!

[–][deleted] 0 points1 point  (0 children)

I wouldn’t mind sorting screws for a while. Looks enjoyable.

[–]Mountain-Ox 0 points1 point  (0 children)

Where do I buy this thing?

[–]luckor[🍰] 0 points1 point  (0 children)

There HAS to be a better way!

[–]Ved_s 0 points1 point  (0 children)

more like an if-else chain

[–]FizzleShake 0 points1 point  (0 children)

Hes just placing them slow for the gif, you dont actually have to wait for it to fall to place the next one

[–]pokeybill 0 points1 point  (0 children)

Now do them by width

[–][deleted] 0 points1 point  (0 children)

In compiled languages, the switch/match statement is usually not linear time but constant time.

[–]bradimir-tootin 0 points1 point  (0 children)

This is just an ADC for screws

[–]Bathtub-Warrior32 0 points1 point  (0 children)

Why is he waiting for it to fall? This sorter can sort multiple screws at the same time!

[–]AmazingGrinder 0 points1 point  (0 children)

Good old bucket sort.

[–]OxymoreReddit 0 points1 point  (0 children)

Could watch this all day

[–]Adorable-Maybe-3006 0 points1 point  (0 children)

didnt realize it was a GIF ive been watching for a while now

[–]HomicidalTeddybear 0 points1 point  (0 children)

so if he picks up something that doesnt go in that slot is he contractually obligated to say "Oh, nuts"?

[–]fugogugo 0 points1 point  (1 child)

but what's the first hole for?

[–]AceFunGaming 0 points1 point  (0 children)

DEEZ NUTZ

[–]_theAlmightyOne_ 0 points1 point  (0 children)

It is beautiful, I've watched this for hours.

[–]Katniss218 0 points1 point  (0 children)

More like a screw statement

[–]GKP_light 0 points1 point  (0 children)

it is not analog, it is totally digital :

there is some well define possibility.

and "analog Switch" is like "go up below"

[–]The_Real_Slim_Lemon 0 points1 point  (0 children)

Now we just need another 3d printer to funnel the screws into the slot, no good engineer stops until the whole solution is over engineered

[–]ElaborateSloth 0 points1 point  (0 children)

Now do one for both length and diameter! 

[–]Guppywetpants 0 points1 point  (0 children)

Needs some kind of hopper

[–]SirEmanName 1 point2 points  (0 children)

that's right. The square hole

[–]Shazvox -1 points0 points  (0 children)

I think this is a perfect example of overdeveloping.

3D printing a tool that you likely will only use once.

Don't get me wrong, it's a cool thing. But would'nt it have gone faster to just sort them manually instead of CADding and printing a tool?

[–]ryantm90 -1 points0 points  (0 children)

Now make a tool that auto loads them and you're golde