Why is it bad? It fulfilled all purposes: It made the developer some (a lot of?) money, a bunch of people very happy, Terraria shares among the most positive reviews of all games on steam (also meaning there can't be too many critical bugs, as that would piss off gamers pretty easily), was/is frequently updated since years, and is generally considered "a good game".
What would a metric such as "clean code score" help with any of this?
The code looks bad. I guess it has been refactored later.
The repo says this code is only for educational purposes. This spaghetti can be still a good example for the reasons you mentioned. Or maybe it is a good example for an ugly prototype version of an game.
This could be even better if people could see a refactored version of this codebase.
Because game code is "throw away" = don't assume you are making a sequel = there's no reason to make it maintainable. You write it as fast as you can to fulfil the requirements. Stuff like unit/system tests are a waste of time because it's cheaper to hire people to manually press buttons.
Yes I know this sounds bad - but that's how the games industry works. And is one of the reasons I left.
Note that if you are working in engine code, then you want all the automated tests, proper architecture, etc.
It's pretty clearly reverse engineered from bytecode by some sort of tool. It's full of `goto IL_label` statements. Would a programmer write a goto in C# at all, let alone call their procedures "intermediate language xyz"?
Yeah, Im not sure why we are talking about decompiled code.
"I know that this is a real mess. But back in the days the decompiler was not so good to create super fancy code and I was to lazy to rewrite thousands lines of code."
From what I can tell this was originally motivated by bugs in the German version of the game. Back then 1.1.2 was to be the last version of Terraria (a running joke at this point, they announced 1.4.4 a little while ago) and so decompiling seemed like the best option to get those bugs fixed. This might be a more interesting blog post on that topic: https://thevamp.cc/2012/07/16/terraria-1-1-2-zu-terraria-1-1...
100% this is decompiled output. There's no way this would be a single file. There's no way there'd be `String.concat(new object[] { "Some", '\t', "strings" })` written by a real human. This is reversed from some bytecode and the decompiler had to make some guesses as to what the original code was.
I'd go further and say "never judge a developer by the code they write without knowing 100% of the context". So many times developers trash each other for shitty code quality without knowing anything about why the code was written the way it was. There isn't always good reasons for "excellent code", sometimes good enough is perfect. We like to think we all live in a vacuum where there is no reason for code to not be perfect, but we forget we live in an environment which is not perfect, and the code is meant to contribute to the this environment, sometimes leading to less-than-perfect code.
So before you judge your fellow developer based on code, ask them first to explain the context around the code, before jumping to any conclusions.
> we live in an environment which is not perfect, and the code is meant to contribute to the this environment
That's a really delightful take! Makes me feel like the code I throw at the wall to see what sticks does make a difference in my corner of the world.
When you add that the definition of perfect can be subjective, one person's definition of perfect might have evolved into it by passing through what is now their good enough definition.
Having looked at a few games' source code, are there really any complex games with pretty code? With as many subsystems, items, and enemies as Terraria has, it feels almost inevitable that you'll have complexity like this snippet for determining when to spawn the hair stylist NPC:
>Having looked at a few games' source code, are there really any complex games with pretty code?
Games don't have the same maintenance requirements as a web application. They are made, shipped, played, and forgotten. So code cleanliness is generally an afterthought. And honestly, most indie game developers are not great programmers. But making a great game has very little to do with programming nowadays. The tooling has come so far that many hit indie games are now built using "no-code" solutions like Unreal Blueprints. And advertising that you hand coded the entire game in C with immaculate coding standards wont make it any more fun.
>Terraria was released in 2011 and is still getting updates.
Yes, it was a runaway success. Terraria is a huge outlier in the indie scene for lasting so long. But my point is that the creator almost certainly was not planning to be maintaining this code 10 years later when he wrote it. And so it was written for speed and simplicity, not elegance and maintainability.
I would recommend anyone in IT to read their blog (and play the game a little for the context) to understand how to wisely deal with complex systems and learn good programming practices.
Since this is decompiled, you really can't infer much from it, in terms of code quality. The original might have really detailed comments, and involve additional functions that have been inlined.
If it's decompiled, then it seems like the decompiled output is a derived work from the binary. And to distribute that derived work on github with a license "Only for educational use" seems like it's infringing. GH should probably take it down.
35 comments
[ 4.2 ms ] story [ 63.1 ms ] threadWhat would a metric such as "clean code score" help with any of this?
The repo says this code is only for educational purposes. This spaghetti can be still a good example for the reasons you mentioned. Or maybe it is a good example for an ugly prototype version of an game.
This could be even better if people could see a refactored version of this codebase.
Source: 12 years in gamedev ;P
- Former game developer
Yes I know this sounds bad - but that's how the games industry works. And is one of the reasons I left.
Note that if you are working in engine code, then you want all the automated tests, proper architecture, etc.
And: https://news.ycombinator.com/item?id=30347289
"I know that this is a real mess. But back in the days the decompiler was not so good to create super fancy code and I was to lazy to rewrite thousands lines of code."
- https://github.com/TheVamp/Terraria-Source-Code/issues/1
From what I can tell this was originally motivated by bugs in the German version of the game. Back then 1.1.2 was to be the last version of Terraria (a running joke at this point, they announced 1.4.4 a little while ago) and so decompiling seemed like the best option to get those bugs fixed. This might be a more interesting blog post on that topic: https://thevamp.cc/2012/07/16/terraria-1-1-2-zu-terraria-1-1...
it would seem decompilers are not that funny.
Don't judge the developers based off of this.
I'd go further and say "never judge a developer by the code they write without knowing 100% of the context". So many times developers trash each other for shitty code quality without knowing anything about why the code was written the way it was. There isn't always good reasons for "excellent code", sometimes good enough is perfect. We like to think we all live in a vacuum where there is no reason for code to not be perfect, but we forget we live in an environment which is not perfect, and the code is meant to contribute to the this environment, sometimes leading to less-than-perfect code.
So before you judge your fellow developer based on code, ask them first to explain the context around the code, before jumping to any conclusions.
That's a really delightful take! Makes me feel like the code I throw at the wall to see what sticks does make a difference in my corner of the world.
When you add that the definition of perfect can be subjective, one person's definition of perfect might have evolved into it by passing through what is now their good enough definition.
In a perfect world, that context would go right into comments next to the code.
Having looked at a few games' source code, are there really any complex games with pretty code? With as many subsystems, items, and enemies as Terraria has, it feels almost inevitable that you'll have complexity like this snippet for determining when to spawn the hair stylist NPC:
Games don't have the same maintenance requirements as a web application. They are made, shipped, played, and forgotten. So code cleanliness is generally an afterthought. And honestly, most indie game developers are not great programmers. But making a great game has very little to do with programming nowadays. The tooling has come so far that many hit indie games are now built using "no-code" solutions like Unreal Blueprints. And advertising that you hand coded the entire game in C with immaculate coding standards wont make it any more fun.
Terraria was released in 2011 and is still getting updates.
Yes, it was a runaway success. Terraria is a huge outlier in the indie scene for lasting so long. But my point is that the creator almost certainly was not planning to be maintaining this code 10 years later when he wrote it. And so it was written for speed and simplicity, not elegance and maintainability.
I haven't seen the Factorio source code, but having read their blog enough, I have to believe they have beautiful source code.
https://www.factorio.com/blog/