Live data from Hacker News

TerrariaClone – An incomprehensible hellscape of spaghetti code

github.com

121–130 of 289 posts

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#121
I have an implementation of Minecraft I'm pretty proud of: https://truecraft.io

However, it is the evolution of several much more embarassing projects. In order from oldest to newest:

https://libminecraft.codeplex.com/

https://github.com/sircmpwn/Craft.Net

https://github.com/SirCmpwn/PartyCraft

https://github.com/SirCmpwn/TrueCraft

I still hate the client code of TrueCraft and it's due to be ripped out and rewritten from scratch. There were also projects earlier than LibMinecraft which, thankfully, have disappeared from the internet.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#122

Earlier quoted context omitted.

Well, it's faster than %, but I bet it's premature.

Why do you say it's faster? It's guaranteed to fail branch prediction one out of ten times. My guess is that'd be a lot slower than using the integer modulo operator, which is not an expensive operation.

On their own, % and / are way slower than +, -, *, >. Cycle counts depend on your architecture, you can look them up. That mod is so slow and should be avoided is a kind of folklore based in truth - kind of like function calls being slow - but like everything time-sensitive the mistake lies in not profiling before (manual) optimization.

There's an example on SO, I got similar results just now when I replicated it:

https://stackoverflow.com/questions/15596318/is-it-better-to...

It doesn't matter on my machine whether the divisor is 10 or 42 (as in the example), the branching is way faster. Now, maybe if the branching were not in a loop and hence not so easily predicted, it wouldn't make a difference. But if this code is not being used in a loop, optimization may be premature anyway (as indicated in my original comment).

Probably f() has something to do inside the main game loop and gets called on a bunch of objects every frame. I haven't looked at the code enough to know if that's a bottleneck.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#123

Earlier quoted context omitted.

Well, it's faster than %, but I bet it's premature.

Why do you say it's faster? It's guaranteed to fail branch prediction one out of ten times. My guess is that'd be a lot slower than using the integer modulo operator, which is not an expensive operation.

At least in C++, GCC and Clang will both use a CMOVE: https://godbolt.org/g/W1GWyP

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#124
post #78

This makes me miss my early days of programming, where no code was too verbose or horrible to stop me from progressing towards my goal, no matter how misguided I was. Nowadays I'm distracted by the first hint that there might be some better way, and all progress stops. I think I'm just beginning to recognize this, and maybe one of these years I'll learn to recognize when the right abstraction is really important and…

Premature abstraction is generally worse than immature abstraction. Write the most straightforward thing you can, and wait for that feeling that it won't work out. Then, ignore that feeling until you get proof. If you've kept your code simple and clean reacting to a lack of abstraction is relatively easy, at least compared to what digging yourself out of the wrong abstraction is like.

YES, this 100 times.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#125
post #66
post #44

Earlier quoted context omitted.

I really like this method: https://github.com/raxod502/TerrariaClone/blob/fd1ff8b8b0e78...

That is probably (x + 1) % 10 assuming x is non-negative. That kind of code is responsible for quite a bit of the verbosity, the author was obviously not aware of many of the little tricks usually used in this kind of code.

Original author here.

It's actually a method to convert from the index of an inventory hotkey slot into the keystroke used to access it -- there were ten hotkey slots, which were numbered 1–9 and 0 at the end.

But I like all the other interesting interpretations here :P (They all assume way more knowledge than I had at the time.)

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#126

This makes me miss my early days of programming, where no code was too verbose or horrible to stop me from progressing towards my goal, no matter how misguided I was. Nowadays I'm distracted by the first hint that there might be some better way, and all progress stops. I think I'm just beginning to recognize this, and maybe one of these years I'll learn to recognize when the right abstraction is really important and…

Something I recently read helped me with this. The guy said “refactor often”. So I code the solution for the problem at hand and when something else touches it, and a refactor is necessary, I do it right then, schedule implications be damned. Early on it can be a hellish bit of regressions, but after a time, the refactors move from less disruptive to smooth. And I get two bonuses: technical debt stays low, and analysis paralysis is optional.

There’s a trade-off between time to completion and designing debt-free code, and I think that one small mantra helped me find the right balance. And let’s be realistic, the only debt-free code is code that is never touched by new implementation.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#127
post #69
post #63

Earlier quoted context omitted.

I disagree. I inherited an app that I'm maintaining that was written by one person over ~12 years. When requirements were added, he just cloned the app and started making the changes so the new app would meet the requirements. Repeat 2 more times, and you get to now, where there are 4 similar but not identical versions of the same code base, with inconsistently applied fixes to various bugs. All 4 still need to work…

My inherited spaghetti codebase took only 24 months for my predecessor to build. I was told: "don't worry, it's in SVN and all of the important switches are clearly laid out at the top." Well, it was in an SVN repo... with a single commit. It did have all the important switches at the top, none of them but the ones that were set actually worked. Man, I "miss" that job.

Was the comment of that commit 'initial commit'? :)

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#128

This makes me miss my early days of programming, where no code was too verbose or horrible to stop me from progressing towards my goal, no matter how misguided I was. Nowadays I'm distracted by the first hint that there might be some better way, and all progress stops. I think I'm just beginning to recognize this, and maybe one of these years I'll learn to recognize when the right abstraction is really important and…

Give yourself permission. I have a GitHub repo called "junkcode" for this very purpose - sometimes projects are promoted out of there, sometimes not.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#129
post #78

This makes me miss my early days of programming, where no code was too verbose or horrible to stop me from progressing towards my goal, no matter how misguided I was. Nowadays I'm distracted by the first hint that there might be some better way, and all progress stops. I think I'm just beginning to recognize this, and maybe one of these years I'll learn to recognize when the right abstraction is really important and…

Premature abstraction is generally worse than immature abstraction. Write the most straightforward thing you can, and wait for that feeling that it won't work out. Then, ignore that feeling until you get proof. If you've kept your code simple and clean reacting to a lack of abstraction is relatively easy, at least compared to what digging yourself out of the wrong abstraction is like.

Indeed, it is concreteness that is the virtue and abstraction that is the necessary evil.

We sometimes lose sight of this because when we learn that abstraction is necessary, and see how beautiful it can be, we forget that it is an extra layer of mental indirection.

Re: TerrariaClone – An incomprehensible hellscape of spaghetti code

#130

I did a 3 month contract working on porting Terraria (the actual game) from XNA to Unity and updating things like UI from sprite batching etc. Coincidentally and fwiw, the actual source code to Terraria was also a hellscape of spaghetti code.

> porting Terraria (the actual game) from XNA to Unity Can you talk about this? What was involved? Why do it? Major roadblocks? Do you feel it was it worth it?

XNA is limited to MS platforms isn’t it? Is imagine it was so they could port/sell to iOS, PlayStation, Mac, etc.
Post reply on HN