r/AskProgramming • • 6d ago

Getting trapped in the Java source code "black hole"

Hey everyone,

I’m currently learning Java to build projects and grind Data Structures & Algorithms (DSA), but I am hitting a massive wall with how the code is structured.

Today, I was trying to implement a simple ServerSocket. To understand what was happening, I instinctively Ctrl+Clicked on it. The IDE immediately unzipped the internal source code and dragged me down a never-ending rabbit hole: from ServerSocket, to SocketImpl, all the way into NioSocketImpl and hidden factory classes.

It throws hundreds of lines of complex internal implementation code at me. It is incredibly time-consuming, frustrating, and completely breaks my momentum. I feel like I'm getting sucked into a black hole of code that I didn't even write, and if I keep this up, I am going to get stuck and fail to actually build anything.

I need a serious reality check and some practical advice:

  1. How do developers actually read and reason about Java without constantly opening up the internal source files?

  2. What is the fastest, cleanest way to look up how to use a class/method without getting bogged down by its underlying implementation code?

  3. How do I force myself to treat things like ServerSocket as "black boxes" so I can just focus on building my app and solving DSA patterns?

Any strategies to help me break this habit and speed up my learning would be deeply appreciated. Thank you!

4 Upvotes

16 comments sorted by

3

u/da_supreme_patriarch 6d ago

First of all, what you are doing in general is actually a good instinct to have, just reading the code in order to understand what it does is most of the times very valuable. However, there are exceptions to this rule and most standard library implementations in any language really(try ctrl-clicking on any STL class in C++, good luck reading any of that code) are not really supposed to be read because they contain tons of platform-specific boilerplate. In your case, socket implementations in Unix systems and Windows differ significantly, and then even in different UNIX systems implementations have their own quirks, which is why it is all abstracted away with platform-specific details hidden away. To not get drowned in the implementation itself, you have to have an idea what the entity that a class is abstracting is supposed to do, and then you hit dot and try to search for a method with a similar name in the autocomplete list, or google "MyClass MyLanguage how to do x" and you'll usually get your answer. So the question here would be - fo you actually know what you want to do with the server socket? If you don't, then you probably want to read ablut sockets themselves. To give you an analogy, assume that you want to use a hash table in some language. The exact implementation of it may not matter initially, and if you know what a hash table is supposed to be doing, you'll search for methods put, get and remove to work with it, or if those are not showing up, google the operation and find the actual name for it. Now, if you have no idea what a hash table does and try to read the code, you'll just get confused by implementation specifics for element ordering, how the buckets are organized and if the implementation is thread-safe - a bunch of sync primitives as well.

Tl;dr - try to understand at a high level what the classes you are trying to use represent, try to search for operations theybare supposed to be capable by just looking for them in the autocomplete list or by googling them, and only start reading the code if you think that something does not behave the way it's supposed to or there is an implementation detail that you are interested in

10

u/regular_hammock 6d ago

I don't know what to tell you except ‘don't do it’. Read the documentation / tutorials. Learn the naming conventions so you don't always need the docs. Maybe you IDE can display little tooltips or such with javadocs for methods and classes?

Right now you're doing the equivalent of ripping off the front panel of washing machine and looking at the circuitry behind to figure out how to operate the thing.

Oh, pro tip: a skill you'll want to build is, how to write your own code in such a way that it has a good programmer interface (that can be used without having to crack it open). This took me years.

1

u/petros211 6d ago

Bruv you shouldn't need to dig into Java source. You will go insane

1

u/Full_School_7230 6d ago

That's what happened with me I made a object of ServerSocket and this class is calling another class ServerImpl and this is returning another object NioSocketImpl while extending Socketopt class🤧😵‍💫

-1

u/petros211 6d ago

The even better advice is to stop using Java immediately and run away, actually

2

u/Full_School_7230 6d ago

That's rude man

-1

u/petros211 6d ago

It is what it is. Trust me I've used Java for many years

2

u/Full_School_7230 6d ago

Then do what? I am a newbie

1

u/ADMINISTATOR_CYRUS 6d ago

I'll be honest and say learning OOP as the first programming paradigm is a terrible idea, and java is a horrible mess of a language and ecosystem. Literally anything else is better. Try Python into C, it's not bad.

1

u/sozesghost 6d ago

Don't listed to that dumbo.

0

u/petros211 6d ago

The thing is that Java will teach you how to code with the OOP(object oriented) mindset and that's a terrible way to code, especially for a new programmer. I would suggest Odin actually

4

u/Raychao 6d ago

The whole point of OOP is Abstraction, Encapsulation, Polymorphism and Inheritance.

You shouldn't need to dig down into each of the internal workings of these classes you should instead only worry about the class surface (properties and methods). The implementation beneath that is abstracted away.

My God man! Never Ctrl+Click!

2

u/ADMINISTATOR_CYRUS 6d ago

I don't disagree, but while that's the entire point of OOP it sometimes is the case that you need to check against implementation details as a reference for what it's actually doing, if the abstracted interface is crap (which happens sometimes).

1

u/LaughingIshikawa 6d ago

How do developers actually read and reason about Java without constantly opening up the internal source files?

Tutorials, mostly.  Also forum posts / stack overflow / friends and coworkers... But most of those can be thought of as different forms of transferring knowledge from one person's head to your head, which works best when it's structured as some sort of learning / tutorial content.

What is the fastest, cleanest way to look up how to use a class/method without getting bogged down by its underlying implementation code?

...Google?  I'm not sure quite what you're asking here. 😅

A quick Google on "How do you implement a Server Socket" brings up this tutorial, for instance.

How do I force myself to treat things like ServerSocket as "black boxes" so I can just focus on building my app and solving DSA patterns?

I'm... Not super sure how to answer that, because I'm not sure I understand why you aren't just doing that?

This is the whole idea of having functions in the first place: so you can call a function without worrying about what it's doing internally (usually) and just pass it the information it needs, and deal with the information it returns.

To borrow someone else's analogy, what you're doing is like ripping open the front panel of a washing machine, in order to trace the exact physical path the electricity takes through machine instead of just adjusting the dials and switches to make the washer do what you want.  I'm not sure how to answer "How do I resist the compulsion to rip the front panel off my washing machine in order to know how it works?" because I am not really sure why you have that compulsion in the first place? 😅

I'm hoping / assuming that you're self taught, and simply don't know yet that this isn't the intended way to interact with core library functions of any language, but like... With this last question especially, it seems like maybe you realize this isn't normal, but you "can't help yourself" in some way?

Arguably IDEs shouldn't allow programmers one-click access to core library functions, because it leads to exactly this kind of problem where beginning programmers think it's more normal and expected to do that in practice than it actually is, but like... In general you aren't going to learn about stuff that way, unless it's some third party library code that's super niche and poorly documented outside of the code itself, and/or when you're doing something really unusual with a standard library function, that requires you to deeply understand the internals 

In the washing machine analogy, this is maybe like ripping open the front panel of an old washing machine so you can wire a makeshift smart home panel onto the side: something you can only do through good (or good enough) understanding of the inner workings, and which is sometimes called for... But usually you shouldn't do unless you're really, really sure there's no easier way. 😅😅

1

u/Intrepid-Anywhere255 6d ago

A practical rule is to read the Javadoc for the public methods first, then copy the smallest official example and get it working before exploring internals. If you still want to inspect source, time-box it and ask one question (for example, how accept() blocks); the JDK socket code has platform layers that are useful later, but noisy while learning the API.

1

u/Recent-Day3062 1d ago

In my way of thinking, no good has ever come from a programming language starting with the letters JAVA first.

And their have been multiple tries