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

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?

→ More replies (2)

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.

→ More replies (6)

-28

u/Candid_Commercial214 Oct 07 '25

there wasn't. having a switch statement that long was the only way

77

u/maikindofthai Oct 07 '25

There’s just no way this is true lol

“I couldn’t think of another way” != “there is no other way”

10

u/Candid_Commercial214 Oct 07 '25

ok well why don't you look at what i was trying to do for yourself

https://docs.google.com/document/d/12V9YLBA1NnpGKhgitz8-5DSfjs_JIKTfemgP26ftWi8/edit?tab=t.0#heading=h.wrhdllbmy9q9

dw if you're not entirely sure what i'm talking about here (it's a keep talking and nobody explodes mod which is pretty obscure) all you need to worry about is that big list of characters and effects near the bottom

34

u/bangonthedrums Oct 07 '25

As a for instance (not necessarily the best way or even a better way, but it is a different way), you could’ve made each character an instance of a class Character, and have an effect() function on the class which is a lambda. You’d have to define each lambda at instantiation but that’s all contained in one spot. Then whichever character you get you just call character.effect(). For maintenance or tweaking, you just have to modify the relevant lambdas

If lambdas are too complex then you do an abstract base class Character that has the effect method as the abstraction. Then you make a child class for each letter which implements the effect method

→ More replies (6)

8

u/littleyrn Oct 07 '25

There are always other ways!

Off the top of my head, say you had 36 "commands" that needed to be registered to a CLI, you could force command registration to happen elsewhere.

Build out an interface with an execute(string[] args), and declare your commands in separate files. From there, theres just about 4 different ways I can think of to register the commands by name to a tree (hashmap of first letter > 2nd letter, so on) and return the correct Command instance or handle if it doesn't exist.

There you go. Easy to maintain, scales basically to infinite command registrations (makes aliases for existing commands SUPER easy), and modifiable during runtime.

No idea what you were doing with a 36 case switch statement, but I'm sure this principle can be applied to that.

10

u/warchild4l Oct 07 '25

Idk about you but for me having 36 separate files is feels much harder to maintain than a switch statement with 36 cases.

13

u/JustAppleJuice Oct 07 '25

To be fair, he was just disproving that the switch statement was the only way

7

u/littleyrn Oct 07 '25

Depends on how much logic each statement has associated with it. For something like a command system, 36 files is definitely easier to maintain. Fuzzyfinding is easier than working with deeply nested switch cases, IMO.

But my point was just that there's a bunch of ways to do things in software.

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.

8

u/evbruno Oct 07 '25

Arrays are such a great data structures! It's constant access time can be precious!

but people can abuse them... maybe this code is ok without a broader context!

OP says it's your own code, but maybe here, you can leverage the knowledge of the context and try some nice refactoring - maybe even micro benchmark this bad boy! Its fine to write "bad code" first, maybe you just wanted to "dump" the idea to the IDE! Now, take your time to improve it.

I vote for 3 out of 10 as a "horror" code (I did way worse last month 🤣)

1

u/Candid_Commercial214 Oct 07 '25

thanks but i already abandoned this

2

u/Gornius Oct 07 '25

Arrays are such a great data structures! It's constant access time can be precious!

That's provided you know the index you're looking for. If you need to search throguh array it becomes O(n), so it's often better idea to use hashmap with key that can hold value that you search for. Then it's O(1).

1

u/evbruno Oct 07 '25

You’re right. I should have said something as “random access”. That’s why we need code reviews. Thanks 😉

1

u/GarThor_TMK Oct 08 '25

No need to search... the array is contiguous... just calculate the position in the array based on the character passed in...

```
if (character >= '0' && character <= '9') return character - '0'
else if (character >= 'A' && character <= 'Z') return character - 'A' + '9' + 1
else throw character is oob.

```

12

u/ahakenab Oct 07 '25

I have an enum with like 150 entries. And a dictionary with the same 150 entries. You gotta pump those numbers up. Thos are rookie numbers!

4

u/Candid_Commercial214 Oct 07 '25

yeah i should time travel back to when John Alphabet was inventing the alphabet and convince him to add my awesome new letters that will be super useful tbh

3

u/ahakenab Oct 07 '25

The germans got a few more äöüÄÖÜß. Somehow they still only count 26 letters in german. I have yet to get answers for my question on that.

2

u/frisk213769 Oct 07 '25

us polish mfs also got a couple extra
'ą ę ż ź ó ś ł ć ń'

→ More replies (1)

2

u/Giocri Oct 07 '25

Add all unicode gliphs to the english alphabet lol

1

u/GarThor_TMK Oct 08 '25

Why would you store your stuff in a dictionary, when the enum is right there?

Is the enum not contiguous? Add a count, and turn it into an array... >_>

1

u/ahakenab Oct 08 '25

The dictionary has strings as the key and has the enum as the value. So that I can call the enum based on a string. I read these values from json files.

10

u/altone_77 Oct 07 '25

Where is horror? Or we talking about unreachable "standards" again?

4

u/Grounds4TheSubstain Oct 07 '25

This is masochism posting, where the OP wants us to tell him what a bad programmer he is. It's not going well!

2

u/Candid_Commercial214 Oct 07 '25

*her

5

u/Grounds4TheSubstain Oct 07 '25

As Michael Jordan said: stop it, get some help.

6

u/Candid_Commercial214 Oct 07 '25

can't post shit on this website huh

2

u/Candid_Commercial214 Oct 07 '25

btw the problem with the switch statement wasn't the fact i was using a switch statement on its own, it was that it was way too damn long for me to put up with. any potential solutions you're offering would probably just have ended up in me making it longer

169

u/kracklinoats Oct 07 '25

a) long array literal expressions are usually a necessary evil, and it’s often nicer to format them line-by-line for readability. No horror here.

b) people shouldn’t be allowed to share their own code as horror. “Look at how bad I did”/“I was so tired last night teehee” type posts are usually super annoying and self-serving. If anything, just do it better and move on.

8

u/gyroda Oct 07 '25

Yeah, it's not quite 100 lines but I've got test methods with a lot of different cases which basically amounts to a very long array of arguments.

It was testing validation of a class with a lot of properties. Each property might have several constraints and there were some constraints across multiple properties (e.g, start date must be before end date).

3

u/Amphineura Oct 07 '25

Sometimes you write bad code and there's no way out lf it, be it a lack of a language feature, bad library API, or pressure from your schedule to put out something bad. I think that's okay.

OP knows it's ugly, but in their case the ugly is probably the best solution, and I think that's a fine submission. It is leagues better than the "I redid Python with C macros!" you see here way, way too often

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Oct 08 '25

When I first saw this I thought it was 108 lines of local variables. I was like "Damn that's a fuck of a lot for one function."

29

u/maikindofthai Oct 07 '25

Idk about these posts where people are sharing their own code. They rarely fit the spirit of the sub imo

80

u/LorenzoCopter Oct 07 '25

Ffs leave piratesoftware alone.

Oh, wait, nvm

17

u/Shapelessed Oct 07 '25

Nah, too few comments and magic numbers in the screenshot to be his...

5

u/SuspecM Oct 07 '25

I remember being so unreasonably pissed off at sonarlint (or I guess they rebranded back to sonarcube?) when I was doing a simple string to date conversion and it called my obvious array numbers magic numbers.

Then the whole PS thing blew up and realised that maybe, sonar was right.

7

u/OutrageousFuel8718 Oct 07 '25

Are you, by any chance, an Undertale dev?

3

u/[deleted] Oct 08 '25

Why do people think long arrays or switch statements are bad? They are good and give good performance, nothing bad about them. 

3

u/thinkingperson Oct 09 '25

Is this game code?

Hardcoding game data instead of storing it in some external filestore? ;)

2

u/LikeASomeBoooodie Oct 07 '25

This is fine dude

2

u/wtdawson Oct 07 '25

I've seen a 108,000 line long variable declaration before lol

2

u/realvolker1 Oct 08 '25

The real horror is that it's an array of booleans. Just use a bitfield ffs, it's basically free performance

2

u/1Dr490n Oct 08 '25

What’s bool[,,]?

1

u/ChaosCrafter908 Oct 07 '25

Are you toby fox?

1

u/pink-ming Oct 07 '25

solution? the problem isn't even presented here. No telling what kind of solution it would be, it just depends on what the switch is doing.

1

u/PEAceDeath1425 Oct 07 '25

And? Its an array, and thats initialization

1

u/HypnoToad0 Oct 07 '25

Using a bool3 array would be better

1

u/RelationshipLong9092 Oct 07 '25

i'm more worried about leaving comments like `// use this for the initialization` on `Start()` than i am hardcoding something like "coordinateGrids"

assuming thats something thats actually reasonably constant and not something that should be deserialized from a data file

1

u/SteroidSandwich Oct 07 '25

why is it a 3d array of bool called coordinatesGrid?

1

u/Reddit_is_fascist69 Oct 07 '25

Just saw my first 4000 line Angular component today.

1

u/festival0156n Oct 08 '25

ok genuine question but how do you guys handle hardcoding large arrays or strings into your program? like it just looks ugly but putting it in another file just feels like added complexity I'm not ready for

1

u/blopgumtins Oct 08 '25

Why dont they store their variables in a database, are they dumb?

1

u/stlcdr Oct 09 '25

Because you don’t need a database for something that is never going to change, and is only used by the app.

1

u/Ronin-s_Spirit Oct 08 '25

I have a yet unfinished and somewhat working JS source parser, it's just a fat switch inside a loop, with more switches or ifs inside many cases, and with a couple gotos to run a few different lines of code depending on how I want to advance the loop. It's quite simple really, nothing wrong with a big switch when it fits.

P.s. to be clear all that itself is written in JS.

1

u/Jelly_Love_CZ Oct 08 '25

If I were to take this fully seriously There is nothing wrong here No single statement is wrong, it's all in building the bigger picture Big arrays can be powerful when used correctly

1

u/Journalist_Gullible Oct 10 '25

I hope the AI gets trained on this code

1

u/GrueneTinte Oct 10 '25

Rookie numbers!

1

u/4Pas_ Oct 10 '25

Tbh this is not uncommon in industry. Encountered one of them during an internship lol, it was almost 600 lines long. Although it wasn't just booleans but constrained a mix of stuff within.

1

u/tomer-cohen Oct 10 '25

Quite curious what's in there can you share what's in those 100 lines? Like the actual code