In re: exceptions in Java. I used SocketServer.accept() the other day. Now, for the unwashed, accept() is a blocking function in an async environment. It's for listening to see if someone connects to the listening port.
And what if -- I did -- you want to listen for this, but in an exitable way, so that, say, if no one connects for a second, you regain control and can do something else, for instance.
Yes, that's correct. Yes, that is the intended usage. Set a timeout with one call, accept with another, wrap it in try-catch.
And I have to say I can't think of any possible reason that use case was designed that way.
You understand? The accept call *blocks*. It's not like we were doing anything else while it's running. We have no choice but to wait for its return.
I can think of at least 5 ways to cleanly implement ServerSocket.accept(xxx) and return a value to indicate that the xxx ran out and we didn't get any connections.
And they chose to throw an exception for that case.
I think it's because some Platonist somewhere thought, "it says exception, and you normally don't want to timeout the accept, cuz it's after all a blocking call, so I guess we'd better throw an exception".
(Side note: accept(), btw, doesn't mean "start listening". The ServerSocket starts listening when it's created. It means, "gimme one thing you heard". ServerSockets have no open or start or such-like, they only have a close. Kickin' it old-school, I spoze.)
• • •
Missing some Tweet in this thread? You can try to
force a refresh
I have been mulling, stalling, hedging, thinking, strategizing, so on and so forth, on a new project I've had in mind, for at least six months.
I think I'm about to pull the trigger on it.
And I'm not even gonna tell you what it is.
Because all the mulling, stalling, hedging, etc etc et fucking cetera had almost nothing to do with the geekery.
I make content for a living, or, more correctly, I make content for a non-living. I have project after project, three of them alive currently, to serve as the base and motive for my content.
Killing time while I wait for my birthday drink(s). I am prowling around a large java code base, looking at code I regard as "good", and partly picking nits and partly re-envisioning it in Kotlin idiom.
It is fun, sometimes, to look at bad code from lousy designers. But I am enjoying looking at reasonably good code from a designer I have a lot of respect for. Feels like it's even more fun.
A f'rinstance nit: don't use inline if's to handle pre-configured optional steps in an algorithm. Use a Strategy constructed at configure-time.