News for hackers. Stuff that matters.
Re:Add detailed message to NullPointerException describing w... by evancox100 on Tuesday March 26, 2019 @12:37PM (#19493412)
> Computation overhead > NullPointerExceptions are thrown frequently.

"Frequently" is obviously a relative term, but are NullPointerExceptions really common enough to be a performance concern? It's good that they are taking performance overhead into consideration of course, I'm just surprised it's even an issue.

Re:Add detailed message to NullPointerException describing w... by bhauer on Tuesday March 26, 2019 @12:47PM (#19493530)
Agreed. I feel the key word in dealing with exceptions is "exception," meaning something uncommon, which is a foreign concept in some codebases, where exceptions are used as routine flow-control.

Throwing an exception in Java is a non-trivial expense since the constructor for Throwable populates a stack-trace structure for the current thread. If you're using exceptions as a routine flow-control mechanism, you're incurring that cost too frequently.

Like you said, sure, tune the performance of this new feature because it's part of the standard library and should be reasonably well-tuned. But if people are frequently throwing NPEs, they should consider alternatives such as simple null checks.

Re:Add detailed message to NullPointerException describing w... by cesarb on Tuesday March 26, 2019 @01:31PM (#19494074)
> Throwing an exception in Java is a non-trivial expense since the constructor for Throwable populates a stack-trace structure for the current thread. If you're using exceptions as a routine flow-control mechanism, you're incurring that cost too frequently.

Unfortunately, the JVM has special code to detect this case (!) and stop recording the stack trace after a while (!!). Then when you go look at your log to see why the system isn't working, you see only `NullPointerException` without the expected stack trace, and you have to guess where it's coming from (after wondering for a few hours why the stack trace went missing, only to find the explanation at https://stackoverflow.com/questions/2411487/nullpointerexcep...).

Re:Add detailed message to NullPointerException describing w... by bunderbunder on Tuesday March 26, 2019 @03:33PM (#19495254)
The fact that anyone ever thought it would be worthwhile to implement an optimization like this is very telling.

I'm not exactly sure what it tells us, but it definitely tells us something.

Re:Add detailed message to NullPointerException describing w... by xkr on Tuesday March 26, 2019 @01:03PM (#19493716)
> exceptions are used as routine flow-control Unfortunately, this approach is encouraged by some popular frameworks, for example a ResponseStatusException in Spring 5
Re:Add detailed message to NullPointerException describing w... by bunderbunder on Tuesday March 26, 2019 @01:17PM (#19493885)
And even the JDK itself. java.lang.InterruptedException comes to mind.
Re:Add detailed message to NullPointerException describing w... by xxs on Tuesday March 26, 2019 @05:37PM (#19496272)
InterruptedException is sort of necessary evil, esp. when it comes to concurrent data structures.

InterruptedException are rare (very) and used mostly to signal termination attempt. LockSupport.parkNanos doesn't throw the exception (unlike sleep, and sleep should never be used in code) but the interruption signal/flag has to be respected somehow. Most of the time is rethrow in library code or bailing out with IllegalStateException in 'userspace'

Re:Add detailed message to NullPointerException describing w... by bunderbunder on Tuesday March 26, 2019 @07:28PM (#19496875)
I think that it could be done better. .NET's CancellationTokens are a step in the right direction, though sadly just one step. You can check if cancellation is requested, but there's also a method for asking the token to throw a CancellationException if cancellation has been requested, and this is how all the abstractions around threading do it.

But it's reasonably easy to imagine ways to produce an API that only uses tokens like that instead of using exceptions to signal cancellation/termination requests. And I suspect, given how many bits have been spilled trying to explain to people just how they're supposed to deal with InterruptedExceptions, that the resulting system would have better ergonomics.

Long story short, I don't know that it's that InterruptedException was a necessary evil so much as that people were still fairly new to this whole exceptions thing when Java came out, and it's really hard to get subtle things like this right on the first try.

Re:Add detailed message to NullPointerException describing w... by paulmd on Tuesday March 26, 2019 @01:05PM (#19493734)
A lot of those NullExceptions (and a lot of boilerplate code) could be linted away if Java gave compiler-level semantics to @NotNull annotations.

Letting null values propagate through your program is frequently a code smell, you want to constrain it at the boundaries of your program logic if possible. Unfortunately, Java does not have a semantic way to do this, just documentation and discipline.

Re:Add detailed message to NullPointerException describing w... by mrighele on Tuesday March 26, 2019 @02:24PM (#19494614)
Checker framework [1] can be used to add compile time checks on such annotations. Unfortunately I found it non trivial to setup (that was some time ago though, I'll have to check again). I noticed also that a recent version of Wildfly application server can do those check at runtime, but I haven't investigated it enough to see if it is configurable and the performance impact of the checks [1] https://checkerframework.org
Re:Add detailed message to NullPointerException describing w... by joe_fishfish on Tuesday March 26, 2019 @02:36PM (#19494741)
Kotlin is amazing for this.
Re:Add detailed message to NullPointerException describing w... by pron on Tuesday March 26, 2019 @02:44PM (#19494823)
As Java supports pluggable type systems, this can be achieved by one: https://checkerframework.org/manual/#nullness-checker
Re:Add detailed message to NullPointerException describing w... by idbentley on Tuesday March 26, 2019 @12:53PM (#19493594)
I've worked with developers who use this pattern frequently for code execution.

try{ .. business logic .. } catch (NullPointerException e) { .. else .. }

Rather than a null guard. That's what occurred to me.

Re:Add detailed message to NullPointerException describing w... by int_19h on Tuesday March 26, 2019 @02:29PM (#19494675)
I've seen a horrifying adaptation of this pattern in C++, where a piece of code was using catch(...) to detect dereferencing an invalid (not even necessarily null, just an object that's gone!) pointer.
Re:Add detailed message to NullPointerException describing w... by lultimouomo on Tuesday March 26, 2019 @06:28PM (#19496579)
That's strange, as dereferencing an invalid pointer in C++ will not cause anything to be thrown. In the best case you immediately get a segfault; if you're unlucky the code just goes on reading random data from memory and crashing some time after that.
Re:Add detailed message to NullPointerException describing w... by int_19h on Tuesday March 26, 2019 @11:13PM (#19497903)
The standard is U.B., so throwing is a valid response.

On Win32, a segfault / access violation - and similar low-level errors, like division by zero - is represented as something called "structured exception". These have a standard OS ABI such that they can be caught, and stack can be unwound, across different languages.

In MSVC, normally, they are not treated as C++ exceptions for the purpose of catching them, although it will still participate in stack unwinding (if something else below is catching them). However, there is an opt-in compiler switch that does make them look like C++ exceptions in a sense that catch(...) will catch them. It's not something you'll see in most code written in the past 15 years or so, for obvious reasons.

Re:Add detailed message to NullPointerException describing w... by marshray on Tuesday March 26, 2019 @08:48PM (#19497294)
Re:Add detailed message to NullPointerException describing w... by arethuza on Tuesday March 26, 2019 @01:00PM (#19493695)
I remember seeing sample code from WebLogic that returned values from methods using exceptions. Mind you this was in documentation rather than in production code - but hardly a good example to follow!
Re:Add detailed message to NullPointerException describing w... by AaronFriel on Tuesday March 26, 2019 @12:59PM (#19493670)
This is arguably more robust, because "foo.bar.quux.doTheThing()" is three potential null pointer exceptions in a row, and the code to do consecutive testing is ugly and verbose.
Re:Add detailed message to NullPointerException describing w... by xxs on Tuesday March 26, 2019 @05:21PM (#19496132)
I guess there are extremely few people in the topic who understand how NPE works internally.

There is no null check in the generated assembly code, when a null dereference occurs - it's an effective kernel trap (as 0 is not mapped to the process). The latter uses the code execution pointer to understand what has been attempted to execute and throws the exception. This may or may not involve stack crawl (which is very expensive) depending on JIT ability to prove if the stack would be unused.

Nulls should be avoided, all fields should be initialized, etc. Nulls are great for =very= high performance code as null checks are virtually free.

Re:Add detailed message to NullPointerException describing w... by DC-3 on Tuesday March 26, 2019 @01:04PM (#19493728)
This is why Rust's '?' operator for error propagation is quite nice.
Re:Add detailed message to NullPointerException describing w... by Filligree on Tuesday March 26, 2019 @01:25PM (#19493996)
Or kotlin's '.?' operator for null propagation.
Re:Add detailed message to NullPointerException describing w... by mbrameld on Tuesday March 26, 2019 @02:31PM (#19494696)
Or C#'s '?.' operator for null propagation.
Re:Add detailed message to NullPointerException describing w... by winrid on Tuesday March 26, 2019 @04:31PM (#19495741)
Or Swift's ? And !
Re:Add detailed message to NullPointerException describing w... by arethuza on Tuesday March 26, 2019 @01:02PM (#19493713)
Having a try..catch round such code would seem sensible but to rely on that over normal checking seems spectacularly horrible.
Re:Add detailed message to NullPointerException describing w... by hamandcheese on Tuesday March 26, 2019 @01:06PM (#19493755)
It’s a code smell if you are calling a method chain that long at all.
Re:Add detailed message to NullPointerException describing w... by AaronFriel on Tuesday March 26, 2019 @01:55PM (#19494352)
This is largely not the case outside of the C-like languages that inherited nulls.

Long method chains with a strong type system or a functional (also called "fluent") style are extremely expressive and common in languages that obviate the need to handle null values at every member.

Re:Add detailed message to NullPointerException describing w... by hamandcheese on Wednesday March 27, 2019 @05:07AM (#19499112)
Regardless of safety guarantees (or lack thereof), a method chain that long indicates a lot of knowledge of the structure and hierarchy of the dependency. It makes refactoring/testing/mocking more difficult.
Re:Add detailed message to NullPointerException describing w... by fgonzag on Thursday April 04, 2019 @08:10PM (#19578407)
What does the length of the method chain have to with anything? Let's say I define an interface Foo.bar.xyz with method call, and I call the implementation foo.bar.xyzimpl.method()

Everything is just as testable and decoupled, it's just a different project structure.

Method chains have nothing to do with the things you stated.

Re:Add detailed message to NullPointerException describing w... by int_19h on Tuesday March 26, 2019 @02:27PM (#19494648)
It's usually a property chain, not a method chain - I don't think get...() is meaningfully a method in most cases, even if implemented as such.
Re:Add detailed message to NullPointerException describing w... by laythea on Tuesday March 26, 2019 @01:51PM (#19494303)
Cannot agree with you more. I personally hate chained expressions. Awful for debugging too.
Re:Add detailed message to NullPointerException describing w... by xxs on Tuesday March 26, 2019 @05:22PM (#19496147)
Java debugging tools are beyond excellent for debugging such code (not that it should be written that way)
Re:Add detailed message to NullPointerException describing w... by laythea on Wednesday March 27, 2019 @02:22PM (#19504206)
I still have to select the sub-expression whose value I wish to inspect. This involves precisely aiming the cursor at text, which is a lot of mental burden (x millions).

A task I thank the developer who creates a variable to hold such a reference, for it creates a much better experience.

Re:Add detailed message to NullPointerException describing w... by wtetzner on Tuesday March 26, 2019 @04:38PM (#19495803)
It's only awful for debugging because the tools are awful.
Re:Add detailed message to NullPointerException describing w... by laythea on Wednesday March 27, 2019 @02:23PM (#19504216)
The tools for Java are greate (try C++ if you disagree).

It's the code that is the problem here.

Re:Add detailed message to NullPointerException describing w... by wtetzner on Wednesday March 27, 2019 @03:24PM (#19504934)
The tools for Java are much better than those for C++. However, claiming the code is the problem is silly. I'm not going to introduce a bunch of local variables just to appease a debugger.

If splitting on separate lines makes code more readable, then sure, do that. But often it just makes the code longer and harder to follow.

Re:Add detailed message to NullPointerException describing w... by liveoneggs on Tuesday March 26, 2019 @12:57PM (#19493647)
I've seen one dev manager (out of many) who failed code that threw NPEs. Every other one let them slide unabated. They are super common.
Re:Add detailed message to NullPointerException describing w... by needusername on Tuesday March 26, 2019 @04:31PM (#19495743)
Well there is an JVM optimization (OmitStackTraceInFastThrow, which is on by default) which optimizes "built in" exceptions that are thrown frequently enough to be cached and have to stack trace. So somebody at one point thought that was a worthwhile optimization and allocated engineering resources to it.

https://www.oracle.com/technetwork/java/javase/relnotes-1391...