Live data from Hacker News

TerrariaClone – An incomprehensible hellscape of spaghetti code

github.com

21–30 of 289 posts

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#21
post #19

The real Terraria code isn't much better. Here's their 3MB [decompiled] NPC.cs: https://raw.githubusercontent.com/csnxs/Terraria/67de21a27e1... They had serious problems implementing multiplayer because they had to synchronize objects with 50KB of state every frame.

Important to note is that this code is not the source code, but rather was generated by dotPeek, a C# decompiler, as mentioned in the repo's README: https://github.com/csnxs/Terraria/

Variable names and comments are lost, but the overall control flow structure of decompiled Java and C# code closely matches the original.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#22
post #12
post #7

That actually doesn't look too bad to me. Sure, it's verbose, repetitive, and deeply nested, but just from skimming it, it looks quite comprehensible. Load all the data from files instead of filling huge arrays and maps in code, abstract similar code into methods, tame the usual mess when dealing with grids with some helper functions taking care of clamping or wrapping around coordinates, replace all the parallel arr…

Truthfully - I wouldn't call it spaghetti code. I think of spaghetti code as way to many abstractions (AbstractEntityFactoryFactory). This code has the opposite problem - needing more abstractions - which is the easier direction to move.

> I think of spaghetti code as way to many abstractions...

That's usually termed lasagna code. Lots of layers with a little bit of filling in between.

https://en.wikipedia.org/wiki/Spaghetti_code#Lasagna_code

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#23
post #18
post #4

Earlier quoted context omitted.

I just scrolled to the bottom and all I see is } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } What the fuck

This is decompiled code, not the original source, so much/most/all of the craziness probably comes from that.

Decompiling .NET or Java binaries usually yields code very close if not identical to the original source code, at least unless an obfuscator was used but that seems not to be the case here. Chances are very good the original source code looks almost exactly like that code, maybe with some additional comments.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#24
post #19

Earlier quoted context omitted.

Important to note is that this code is not the source code, but rather was generated by dotPeek, a C# decompiler, as mentioned in the repo's README: https://github.com/csnxs/Terraria/

Variable names and comments are lost, but the overall control flow structure of decompiled Java and C# code closely matches the original.

Even variable names are usually not lost.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#26
I did the same trying to write a Minecraft clone back in 2012: https://github.com/beager/craftalike

I'm very much not proud of the code, it's sloppy, incomplete, quite copy-pasta. But it helped me learn a lot of concepts about game dev that a web dev wouldn't know, and it was tremendously exciting to create something in code that you could compile and play around with. No regrets.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#27
post #12
post #7

That actually doesn't look too bad to me. Sure, it's verbose, repetitive, and deeply nested, but just from skimming it, it looks quite comprehensible. Load all the data from files instead of filling huge arrays and maps in code, abstract similar code into methods, tame the usual mess when dealing with grids with some helper functions taking care of clamping or wrapping around coordinates, replace all the parallel arr…

Truthfully - I wouldn't call it spaghetti code. I think of spaghetti code as way to many abstractions (AbstractEntityFactoryFactory). This code has the opposite problem - needing more abstractions - which is the easier direction to move.

Global state causes code to be spaghettified. Having worked on several code bases that qualify as spaghetti code, the hallmark trait is that the execution flow is wound back around itself many times, like noodles in a bowl of spaghetti, in a way that's not easy to trace. You may have nice abstractions or none. You may have deeply nested type hierarchies or a seemingly nice flat structure. You may be using an IoC container with neatly separated services. None of it matters, you can get spaghetti code with all of them.

Just use global state! Then you can have something where Module A depends on Module B. Now, at one point Module A calls into Module B which fires off an event that's handled in Module C that then calls back into Module A. If that event is going through a pub/sub notifier, bam, hidden global state. Good luck tracing that subtle bug down when you swapped out implementations of Module C thinking the new one explicitly filled the contract of the old one, or worse yet not realizing Module B indirectly depended on C in the first place, and that A's response to the call from C may repeat the cycle several times. Soon you realize everything depends on everything else and your tooling does you no good. That's spaghetti code, just as bad or worse than the gotos sprinkled through some ugly C code that an undergrad wrote in the 80s.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#28
post #5

The real Terraria code isn't much better. Here's their 3MB [decompiled] NPC.cs: https://raw.githubusercontent.com/csnxs/Terraria/67de21a27e1... They had serious problems implementing multiplayer because they had to synchronize objects with 50KB of state every frame.

Wow, I just glanced at the how they're generating random names and it's incomprehensible. What a bizarre way to do something rather simple.

My guess is they are using partial classes and generating a bunch of the code from other, much simpler files.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#29
post #25

I think true spaghetti code requires teamwork. I mean that seriously. You need at least 3 people all with different incomplete and incorrect mental models trying to modify the same codebase at the same time.

I agree. But then again, I'm like a completely different programmer than I was a few years ago, and even more different than I was years before that. I've got some long running personal scripts and projects I tweak every so often, and the end result is pretty much the same. "What the hell was I doing here? Whatever... I'll just add a bit of code and get it working. Done."

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#30
I'd actually try to recruit raxod502 at the high-school senior level. It certainly shows passion and commitment. You just need to grok the higher level abstractions. And may in the end even find your "spaghetti" version is actually more performant at run time ;)

The thing is you sort of need to write like this for the first draft of your first game. And Terraria is pretty ambitious. Considering most people struggle with BlackJack or Pong or Snake!

Post reply on HN