News for hackers. Stuff that matters.
Re:A Reply to “Let’s stop copying C” by iainmerrick on Saturday January 25, 2020 @06:46PM (#22149386)
If when you say “null pointer exception” you’re thinking of Java: do you find any value in nullable / non-null annotations? Or if you’ve used Kotlin, its non-nullable references?

I find them very useful. I don’t just want null pointer exceptions to be easy to debug, I want to avoid them completely in the first place.

Re:A Reply to “Let’s stop copying C” by chongli on Saturday January 25, 2020 @07:16PM (#22149525)
You're right. The mistake is not in having null pointers, it's lacking non-nullable references which is the problem. If you can write a function which only accepts a non-null reference, then you don't have to check in every function called from it.
Re:A Reply to “Let’s stop copying C” by AgentME on Sunday January 26, 2020 @12:45AM (#22150583)
+1. I think this is something that gets lost in discussions around null. Generally, everyone complaining about C's handling of nulls will recommend languages that make nullable-pointers opt-in by having non-nullable references and making them the default. The argument people make is not that C should somehow drop the entire concept of null; the argument is that people should use languages where a type has to opt-in to accepting null.
Re:A Reply to “Let’s stop copying C” by tlbsofware on Saturday January 25, 2020 @06:55PM (#22149420)
A really effective way to handle nullable fields is an Optional<T> which forces the user to handle a null value for that field, this is very useful when working in a team and dealing with objects that someone else spent time creating, it won’t get rid of null but it definitely hints the user at “hey this field may be null so I should go ahead and handle this before I even get to testing my code”

Edit: You would want to implement these in your getters for fields that may return null

https://docs.oracle.com/javase/8/docs/api/java/util/Optional...

Re:A Reply to “Let’s stop copying C” by einpoklum on Saturday January 25, 2020 @06:56PM (#22149423)
Note that in C++ today, static analysis tools + classes such as `std::unique_ptr`,`gsl::owner` and `gsl::non_null` (similar to the Kotlin mechanism you described), it is possible to avoid the possibility of null pointer dereferencing.

That's not to say the same thing is possible in C, but one _can_ go a long way with static analysis at least, and suspicion vs provability regarding pointer dereferences.

Re:A Reply to “Let’s stop copying C” by dehrmann on Saturday January 25, 2020 @07:16PM (#22149527)
> do you find any value in nullable / non-null annotations

Hah! Annotations. They're tales told by idiots, full of sound and fury, signifying nothing.

But seriously, they're about as meaningful as reading the javadoc. At least Optional enforces the contract; it's just clunky.

Re:A Reply to “Let’s stop copying C” by iainmerrick on Sunday January 26, 2020 @04:54AM (#22151320)
You can configure your IDE to warn of incorrect usage, and you can configure your compiler to flag warnings as errors. That will catch a lot of potential problems at compile time. It’s not perfect but it can be lot better than nothing.

Java annotations are better than Optional when you’re interoperating with Kotlin code, such as when you’re partway through converting a large codebase to Kotlin.

Re:A Reply to “Let’s stop copying C” by smoyer on Saturday January 25, 2020 @07:51PM (#22149662)
@NonNull etc are not completely meaningless ... if you run SpotBugs it can be set up to fail your build if the contract specified in the annotations isn't met. I love static analysis!