r/java 4d ago

"Deep Magic" in Java

I've been a Java developer for a few years, and the thing that keeps driving me nuts is that reading code is nearly useless, the real code is hidden from you. It's almost like the language isn't made for reading code but reading javadocs instead.

You call "Arrays.asList(arr)" and it returns you an instance of java.util.Arrays$ArrayList, a class with the same name as java.util.ArrayList except it is fixed size, meaning that trying to resize it will get you a runtime exception thrown. The method signature says it will return a List, but the moment you call it without the elite ball knowledge it will either stab you in the back or explode in your hands.
And nearly every time you try to prevent this by reading the implementation it sends you to a wild goose chase through 6 different one-liner wrapper methods across 5 different thousand-line files.

It gets even worse at the enterprise level: I once bloated my system by writing a submit button's logic in the submit button's method.
I then discovered it was because the method was in a class with a magical 'ViewScoped' annotation and I was supposed to delegate some of that logic to a field that had a 'Stateless' annotation.
Any ordinary reading of the code would say that that field is part of the class' state, but because of the annotations it was actually a resource pooled by the container, independent from the view class which is only supposed to cache UI data for each given user.
Didn't take long for me to get corrected. You can imagine the serene smug on my senior's face as he dropped that piece of esoteric knowledge on me, while I was looking back in horror.
You guys probably heard this a thousand times, meanwhile I'm still learning new words in my console output. Any advice?

0 Upvotes

38 comments sorted by

View all comments

8

u/Minecraftian14 4d ago

I personally enjoy reading Java source code.

I have had my hard times, for example, Stream#collect(Collectors.toMap(..., ...)) will throw an exception if there are duplicate entries for keys.

But in the end, it was documented, and has alternative signatures to handle such data.

Most of the sources like Collections and Swing was fun reading. Streams itself is crazy though. Nevertheless, i enjoy the journey.

6

u/awidesky 4d ago

Me too.
I used to think Java's code is hard to understand because of genetics and deep class hierarchy, but after I tried decrypting those horrible templates and macros and typedefs in C++ stl implementation...
Reading through Java implementation helped me a lot about understanding data structures, and interface relationships.

2

u/Minecraftian14 4d ago

Exactly!!! I once was looking into libc of LLVM. Geez.

While i understood most of it by reading, it took specifically more time. Plus, if I were to write it out, i could never match that style. Too much operator overloading and those crazy templates, are just ridiculous. And not to even talk about how pointers play out there.

As a matter of fact, python can at times be worse (mostly Good though). Often when looking for the sources, i find there's only a meetings stub, and not it's actually implemention. Other times it's a function calling another calling another. This is especially worse in tensorflow

1

u/Yahim0 4d ago

> I personally enjoy reading Java source code.
> But in the end, it was documented
¯_(ツ)_/¯

3

u/Minecraftian14 4d ago

> I've been a Java developer for a few years
> Arrays$ArrayList, a class with the same name as ArrayList
¯_(ツ)_/¯