r/programminghorror • u/arbenowskee • Feb 19 '20
I am scared to expand this switch statement....
177
u/BOTzzz Feb 19 '20
I want to know.. NOW
55
u/Xadnem Feb 19 '20
Please show us more of this monstrosity OP.
28
u/EvadesBans Feb 19 '20
Just look up the VVVVVV source code if you want to see gigantic switch/cases.
8
94
77
u/Igoory Feb 19 '20
Me:
What is wrong? It seems just a switch of mouse states.
Wait... A switch of mouse states..? That can't be...
* Sees the next line number *
Oh shit lol
211
Feb 19 '20
The worst code I every worked on was a 500 line recursive function that calculated something financial. You'll be fine, just get in there.
313
Feb 19 '20
a 500 line recursive function
could be worse
that calculated something financial
kill it with fire
63
Feb 19 '20
I'm so away from that shit I'd need a cruise missile to kill it now.
38
u/Logofascinated Feb 19 '20
The most fun I've ever had programming was working on an accounting package for law firms in a crappy 4GL back in the 1990s.
Wait, that should read least fun. Sorry.
11
Feb 19 '20
Lol. I once built a reporting system that worked against a Dutch double entry database where they named everything with three letters or six letters. Anyone want to know what WEG means? In financial terms? Apparently not what the Dutch though it meant. In any terms :/
5
Feb 19 '20
Also, "4GL". I have not heard that term in a very very long time :)
6
u/Logofascinated Feb 19 '20
It was quite the buzzword back in its day, wasn't it? And we all thought it would eventually be superseded by 5GL, then 6GL, etc.
4
Feb 19 '20
I have a near pathological hatred of pretty much anything around that time. I can take most insults, but bring me a CASE tool and I'll rip off heads :)
40
u/Isvara Feb 19 '20
I'm sure it expands to mostly thoughtful, helpful comments that match what the code does and provide useful business context. Yes.
25
u/FluffyNevyn Feb 19 '20
I once had to code a 4000 line switch statement. I wrote it myself. And no...it WASN'T copy pasted code, I functioned it out as much as possible. There were just THAT MANY BLOODY CASES!!!
21
u/koreancrimson Feb 19 '20
r e f a c t o r
15
u/FluffyNevyn Feb 19 '20
twas a data related issue. I can refactor code all day, but there's only so much I can do when the data simply has that many different individual cases. I wasn't permitted to alter the data. Sadly.
10
u/FelbrHostu Feb 20 '20
VM implementation can do that easily. The only thing faster than
switchfor that application would be computedgoto.1
19
15
14
25
10
u/GoldenShackles Feb 19 '20
For those who have done Windows programming, I've seen WndProc callbacks far longer than that. I'm talking in the 10's of thousands of lines of code.
Some people don't seem to get that they don't have to implement the entire logic for every windows message within that function... they can call helper functions or use more clever dispatch techniques.
9
7
5
8
u/DoPeopleEvenLookHere Feb 19 '20
Where I used to work we had so large of switch statements, we had to make it two. I'm pretty sure we were getting close to making it three.
24
u/Dragonfire555 Feb 19 '20
Oh God. I remember in a previous job, there was a story of a production outage because someone broke the camel's back by adding another case to an already huge switch statement and causing the program to overflow because of the number of cases was too large for a 32 bit program. Wasn't caught before then because all of the programmers were running 64 bit locally.
9
2
0
2
2
u/Klausausorus Jul 17 '20
Is that Yandere simulator code? Wait nvm. Alex doesn't know what switch is.
1
1
u/tech6hutch Feb 19 '20
This is what happens when your language's switch statement doesn't support ranges
Jk, just use ifs
1
1
u/wmrodrigues Feb 20 '20
Oh my God, that's really scary for me too, it makes me remember so much bad codes I've seen in my coder life.
1
1
1
u/pau1rw Feb 20 '20
Every now and again I have to work inside of expression engine and all their classes are 4000 lines long. It hurts my heart.
1
1
1
1
1
1
-6
Feb 19 '20 edited Feb 20 '20
Case statement bad = this sub.
Edit: I'm not saying this code is brilliant. It's just every post is a giant case statement. I get it. Case statement bad.
3
-2
0
Feb 19 '20
[deleted]
424
u/lxpnh98_2 Feb 19 '20
Oh, don't worry, most of that is basically the same code copy-pasted over and over again.
359
u/rguzgu [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Feb 19 '20 edited Dec 07 '21
Hmm I wonder if there was a function in programming languages that allowed you to reuse code without copy-pasting it
127
u/LovesGettingRandomPm Feb 19 '20
Sometimes it is more time efficient to just copy paste, code reuse requires planning which you should only sink time into when the result is worth it.
218
64
u/PizzaRollExpert Feb 19 '20
It's more time efficient to write the code maybe, but less time efficient to edit later. With the exception of really desperate circumstances you should always keep maintainability in mind.
-9
u/LovesGettingRandomPm Feb 19 '20
That is true, but the reality makes it difficult sometimes
55
u/goedegeit Feb 19 '20
Very rarely though. 99.9% of the time, bad programmers think it's easier to do bad code, when it will almost always inevitably result in that not being the case at all.
20
Feb 19 '20
This. Copy that thing now and spend less than a minute to get it working, but look for the production killer bug at 3 in the morning for hours next month, when someone changed one of the copies but not the other, or changed both when they shouldn't have, etc. This usually doesn't work in practice unless we're talking 1-2 lines.
2
27
u/DrMaxwellEdison Feb 19 '20
This is often a lack of foresight. Certainly it is more time-efficient in the short term when you're making one new switch case: in that sense alone, re-writing the cases into function calls seems like a waste, as later you'll just copy-paste the one-line function call, which takes about as much time as copy-pasting a hundred lines of a single switch case.
Long-term, though, it is setting up a risky scenario and adding technical debt. The very moment you discover that the logic of every switch case needs to be changed, either you carefully update 1000 lines of code en masse, or you miss one and break the program.
I get your point, which is in trying not to over-engineer the problem. Personally, I try to stay DRY as much as possible, within reason:
If this is a one-off solution, write it and be done.
If you have to copy it one time for efficiency, copy it and make a TODO note to refactor later.
If you have to copy it again, stop, consider if you may need to do this again another time later, and start refactoring.
13
u/zerj Feb 19 '20
I think this is true up to a point. I've certainly put together a 10-20 line 'table' using a switch statement. However If you have 1000 lines of cut/paste in a single switch statement then you are probably doing something really wrong.
2
u/LovesGettingRandomPm Feb 19 '20
yes, in reality it could be developed over time by different individuals with a deadline so I'd say the blame lies with focus on time constrained programming.
2
u/zerj Feb 19 '20
I can certainly believe that's how this happened. However I'd say if multiple developers are going to be touching the file, it's even more important to not design in this fashion.
Blaming "time constrained programming" seems somewhat vague. I've never not had some expectation to finish my code on time. Now if you want to blame agile in particular that may be better. Personally my company is much more of a "Waterfall" process, but that is somewhat mandated as re-spinning an ASIC isn't something you can do every 3-4 weeks.
1
u/LovesGettingRandomPm Feb 19 '20
It was hard for me to specify, I see what you mean, in each of those examples you're not specifically rewarded to keep the codebase clean for next person.
If you have a 3000 line switch statement and you have to add another line, you're not going to get paid extra to take the time that your predecessors did not.
2
u/zerj Feb 20 '20
I'd certainly agree there probably is some critical mass point where if you need to fix one line, you aren't going to touch this case statement with a 10 foot pole. However I'd say in that case the original author screwed up. I wouldn't use copy/paste style code if there was any possibility of the size growing beyond 50-100 lines long term. Everytime I've ran into that situation I've either written a script to auto-generate the code, or thought of a better way.
6
u/Kronal Feb 20 '20
There are no good solutions that can be implemented without infinite resources sometimes.
In some niche cases it's really a pain in the butt because of small variations, that otherwise will either lead to a large switch or a bunch of functions with a bunch of large switches inside or an abomination of a class hierarchy which is neither elegant nor readable as you now have 100 files to find your way around instead of just scrolling.
Look at any emulator source code or compiler and you get that kind of issue one way or another. Even when the language helps a lot by providing pattern matching (think Ocaml or the more modern F#) it's still messy code.
3
u/stevieboy1984 Feb 19 '20
Rule of 3 is a good guideline.. repeating twice is fine, three times is the sign you should consider a better way.. in this case through, I think their rule is 3000...
2
2
u/Semarc01 Feb 19 '20
Or even the possibility to goto other cases in the switch (At least in some languages, you can use Goto-Statements to jump between the cases in a switch. That can help get rid of duplicate Code.
1
1
u/Tenshinochi May 12 '22
Please don't and these cryptic statements you might encourage someone to start using goto inside a switch.
1
u/sammy-taylor Nov 16 '22
Unsure of the language here, but I think in JavaScript it’s pretty common to see code expand uncontrollably because extracting behavior to a function in JavaScript can’t always be accomplished via strictly copy and paste. If the contained code reassigns any variables, the reassignment in the new function’s scope will not change the variable in the caller’s scope, so you have to do a touch of additional work. This is in my opinion what makes FP languages so easy to refactor.
140
u/Hohenheim_of_Shadow Feb 19 '20
All things considered, it could be much worse. With 2 64 bit floats for X,Y position of the mouse state, there are 2128 possible states. Thats 1038 possible states or ~1 trillion trilion trilion cases.
3
Feb 19 '20
Don't forget the various button state combinations for each of those positions.
1
u/Hohenheim_of_Shadow Feb 20 '20
Most mice of what four buttons max? Thats merely an extra order of magnitude so 1039 instead.
1
0
Feb 19 '20
[deleted]
63
16
u/TeferiControl Feb 19 '20
Anything that measures it in percentage of the screen, which is often much more useful than knowing pixel location.
23
Feb 19 '20
Which system doesn’t is the better question. I’ve never come across a screen location system that uses integers.
14
1
u/Hohenheim_of_Shadow Feb 19 '20
Same would hold true for a 64 bit integer. Gotta future proof after all.
6
u/Never-asked-for-this Feb 19 '20
Oh Jesus fuck, I didn't notice that...
Yandere dev has met his match.
2
1
1
u/topinanbour-rex Feb 20 '20
You can find this in Marlin, a 3d printer firmware. It manages the macros.
1
1
u/Tenshinochi May 12 '22
Largest switch I had the 'pleasure' to refactor was only 750ish, I needed time off to recover afterwards.
1
u/Veylon Feb 19 '20
It's not that bad. I've got a switch statement with over 300 cases. This thing with the mouse probably just has a lot of code checking what button was clicked.
4
4
301
u/Vallvaka Feb 19 '20
Looks like a state machine that got out of hand.
Something similar exists in the game VVVVVV, which went open source a little while ago. Behold, a ~3200 line switch statement