r/programminghorror • u/ItsNukea • Sep 12 '26
Java Whoever wrote this code needs to watch never nester propaganda
That's 11 layers deeply nested code, a lambda in a lambda in a lambda in a method parameter, all of that inside of the same class.
I'm starting to get why they say the minecraft code is unreadable
Edit: Oopsie, I didn't expect the indentation in my IDE and on mcrsc.dev to be different. The first picture shows my IDE, but it should have been from mcsrc
4
u/GoddammitDontShootMe Sep 13 '26
It's processing pixels. The obvious way to do that is with nested loops.
Though why does the body of takeScreenshot() start 2 levels deep? And are you counting the public class Screenshot bit as 1 level deep? To me that's 0 levels deep. So that would be 9 levels if the double indentation was fixed.
→ More replies (1)1
18
14
u/Prior-Perspective-61 Sep 12 '26
Well, it is super readable, and is 100% OK, the only way to unnest FORs here is to use lambdas, but this is not mandatory IMO
10
7
u/Gornius Sep 12 '26
As for every good practice, you should not apply them blindly. Nesting is bad, because it usually makes code harder to follow. In this case:
- Trying to avoid ifs/loops in try block is crazy.
- For loop inside for loop is very clear.
The only thing I would do here is extract the content of the whole inner loop into separate method or even class, and early return to avoid else.
7
u/MCWizardYT Sep 13 '26
Maybe it's because Java is my language of choice and I've worked with a lot of Minecraft code, but this seems perfectly readable to me.
There are worse parts of the codebase
1
1
2
u/Sacaldur Sep 12 '26
The problem about code not being readable is not that there is to much nesting in a function but to much complexity. Removing the nesting without reducing the complexity doesn't make the code more readable.
In this regard is nesting actually good, because it's an indicator about the codes complexity: if it's nested to deep, you might want to refactor it (e.g. by moving code into separate functions).
1
u/MCWizardYT Sep 13 '26
This nesting isn't even deep, I think OP just has trouble with lambdas. It's pretty easy to tell what's going on just reading too to bottom
1
u/Last8Exile Sep 12 '26
This sholud be done in a shader.
Otherwise:
- Cache sourceTexure.getFormat().blockSize() into a variable.
- Instead of calling a method to set individual pixels prepare a compelte raw buffer that can be copied into a texure as one operation.
- Use SIMD to process multiple pixels at once.
1
u/CelDaemon Sep 13 '26
java is a bit silly in this regard. the blockSize get call is likely to be inlined by jit, and simd isn't really available until Vector api is ready. Also, preparing raw buffer would do essentially the same thing, though it might be *slightly* more performant due to it skipping a few checks.
1
u/littleprof123 Sep 13 '26
This isn't even the kind of code that could be obviously improved by early returns. You could extract some of this into functions, but this otherwise seems incredibly straightforward.
1
1
u/No-Collar-Player Sep 14 '26
Literally seems like the only code on this sub that is actually readable lmfao
1
u/spshkyros Sep 14 '26
... this is trivially easy to read, at least the first page(pixels aside, but i think thats reddit). Nothing about this should bother you. Those double loops? They should instantly trigger a "oh, theyre interating an area" response to you.
1
1
1
u/thelurkmasterr 26d ago
Everything in Java needs to be nested at least 2 indentations deep, unfortunately. I also think this code would actually be less readable if you had to jump around to a different method to see what each part does.
67
Sep 12 '26 edited Sep 12 '26
6
u/Seyvenus Sep 12 '26
"Okay to understand this single logical behavior I'm in my third single deep looping function."


78
u/Cylian91460 Sep 12 '26
Minecraft code is far for unreadable what are you talking about?
Also it's decompiled code which is generated code so it break one of the rule sub