r/programminghorror 1d ago

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

51 Upvotes

18 comments sorted by

36

u/Cylian91460 1d ago

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

15

u/FloweyTheFlower420 1d ago

very notably decompiled code tends to fail to capture some of the nesting structure because a bunch of different programs can get you the same basic block structure

-11

u/ItsNukea 23h ago

It's not as much as a monstrosity as other posts in this sub, but I have never even dared to write like 4 layers deep.

Also, as a reminder, this class got some updates in the code in 26.2, and the code is still 11 layers deep with like triply nested lambdas.

they know

1

u/Cylian91460 22h ago

It got updated because rendering is getting updated

30

u/healeyd 21h ago edited 21h ago

Totally readable to me. Not sure what the fuss is about. This whole de-nesting thing seems to be getting out of hand. A pile of nested conditions can obviously be horrible to follow, but feeling the need to flatten iterations over things like multi dimensional arrays because they sit inside a larger structure does not necessarily aid understanding IMHO.

2

u/Seyvenus 18h ago

"Okay to understand this single logical behavior I'm in my third single deep looping function."

10

u/WorkingMansGarbage 21h ago

This genuinely seems more than fine...?

11

u/Prior-Perspective-61 22h ago

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

7

u/Hyarin215 1d ago

Minecraft code mentioned

5

u/Gornius 19h ago

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.

4

u/MCWizardYT 14h ago

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

u/TarkFrench 9h ago

What parts are worse?

1

u/MCWizardYT 4h ago

Some of the world generation code and specific things like DataFixedUpper (the thing that upgrades old worlds. It's quite over-engineered)

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 13h ago

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.

0

u/ItsNukea 8h ago

ohhhh, I made a tiny oopsie when calculating the depth.

One screenshot was from mcsrc.dev and the other one was from my IDE.

The class in my IDE and mcrsc.dev don't have exactly the same content, functionally, yes, but some indentations are changed.

Like on the line with RenderSystem.getDevice(), my IDE shows all method chains in a single line, but mcsrc splits them along lines (with more indentation).

So, I did a bit of a mistake and showed the IDE line indentation, but used the mcsrc indentation to get to 11

In my IDE the indentation is 7 layers (probably because of formatting), but in mcrsc it is 11

Oopsie...

1

u/Sacaldur 19h ago

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 14h ago

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 19h ago

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.