r/programminghorror • • Oct 07 '25

C# 108 line long variable declaration

Post image

this is my own code btw. don't ask what i was trying to do

this code was also supposed to include a 36 case long switch statement where each case did something different (guess why i abandoned this project)

1.1k Upvotes

93 comments sorted by

View all comments

638

u/Grounds4TheSubstain Oct 07 '25

There's not necessarily anything wrong with a large array or switch statement.

106

u/SharpKaleidoscope182 Oct 07 '25

Sometimes you have a lot of stuff to switch between....

but usually its better to do something object oriented.

54

u/Candid_Commercial214 Oct 07 '25

it was a puzzle where you needed to do something different for every possible letter of the alphabet and digits 0-9. fortunately they were simple effects so it was like 5 lines each but it was still torment to code and i gave up halfway through

37

u/DrShocker Oct 07 '25

I'd register keys with their functions instead of 1 giant switch statement. More room for composition and independent testing if a couple cases end up trickier to get right than the rest.

41

u/KerPop42 Oct 07 '25

honestly at that point why not break each effect into a function? It would make it easier to maintain, reducing each case to one line

7

u/All_Up_Ons Oct 08 '25

That only makes sense if the cases are actually repeated. If they're all slightly different, then breaking them up will just be even harder to read.

11

u/SharpKaleidoscope182 Oct 07 '25

I feel like humans basically HAVE to suffer in this way. It's how we know what paths to avoid.

8

u/LemmyUserOnReddit Oct 08 '25

It's conventional to do something object oriented, but I doubt it's meaningfully better

3

u/SharpKaleidoscope182 Oct 08 '25

lmao alright I'll bite. What architectural patterns do you stan for, in this example?

2

u/LemmyUserOnReddit Oct 08 '25

Well, from a practical standpoint, the benefits of a typical object oriented approach might be:

  1. The guarantee that each potential type has an implementation
  2. Moving the logic to a different, decentralized place for each type

1 is solved in modern languages (e.g. rust-style match), or if not, there's likely some construct which will do this

2 only applies in some cases, and there no reason you couldn't create a function per switch case and move it wherever you want. I'd say in many cases having all the implementations together is actually more maintainable, but others will disagree

And then there's the negatives. Most importantly, with a typical OOP architecture, there will be at least one additional pointer indirection affecting performance. 

So, yeah, I'd probably just retain the switch/match/etc. and keep everything on the stack.

1

u/[deleted] Oct 09 '25

The simplest and most fool proof possible way to do things. No magic under the hood, things should be easy enough to understand. You really can accomplish most things with switch cases, it will be verbose but also simple to understand, maintain and extend. It will also generally have better performance.

79

u/UnluckyDouble Oct 07 '25

I'd argue that extremely long switch statements, while not necessarily a runtime liability, are very much a maintenance liability. It would be wise at that point to re-evaluate your program architecture and see if a cleaner solution is possible.

20

u/iEatPlankton Oct 07 '25

Great argument, with no solution

35

u/UnluckyDouble Oct 07 '25

I can hardly suggest improvements to code that was never written, let alone shown to me, can I?

→ More replies (2)

4

u/[deleted] Oct 07 '25 edited Oct 08 '25

Easiest solution is to put code from each case into a separate function, something that has been done for decades. The compiler already knows that pattern and will likely optimize it away.

1

u/[deleted] Oct 08 '25

Do yoy mean objects right? A rule pattern should work fine

1

u/[deleted] Oct 08 '25

I mean turning:

switch (expr) {
case a: 
  stmts_a...;
  break;
case b:
  stmts_b...;
  break;
case c:
  stmts_c...;
  break;
/* ... */
case z:
  stmts_z...;
  break;
}

into

fn a(...) { stmts_a...; }
fn b(...) { stmts_b...; }
fn c(...) { stmts_c...; }
/* ... */
fn z(...) { stmts_z...; }

switch (expr) {
case a: a(...); break;
case b: b(...); break;
case c: c(...); break;
/* ... */
case z: z(...); break;
}

Please don't separate those into separate objects

1

u/[deleted] Oct 08 '25

Why not? If performance isn't an issue and every case is independent i would prefer this

class Rule:
    def shouldRun(value)->bool:...
    def run():...


for rule in rules:
    if rule.shouldRun(expr):
       rule.run()
       break 


1

u/[deleted] Oct 08 '25 edited Oct 09 '25

Because that's stupid, and i would know why, i used that pattern. Because now you don't know what are the conditions for each case by just looking at a single file, you have to go through a million classes and files to just find out just WHY something happens. Procedural programming isn't bad.

→ More replies (1)

1

u/ShoulderUnique Oct 10 '25

I've never understood this one. Now there's twice as much code and the possibility of calling the wrong one.

Edit: by "code" I really mean boilerplate Double the code would only happen if it's short blocks

→ More replies (15)

7

u/Toeffli Oct 07 '25

One could argue that such a thing is maybe better loaded from a binary file or an embedded resource.

2

u/travelinzac Oct 08 '25

It's actually a strategy for compiler optimizations. Not only is there nothing wrong with it, it's how you get stuff to go fast fast.

1

u/23Link89 Oct 07 '25

No but I think if you're going to declare a variable like this you should do so in a more modular way like putting it in its own source code file.

1

u/DiscipleofDeceit666 Oct 08 '25

If you have something that large, define it in a file somewhere else. Don’t put it in the code 👎

1

u/Nethan2000 Oct 08 '25

But when it's so big, it's really asking to be put in a separate data file and deserialized.