News for hackers. Stuff that matters.
Re:Const all the things? by quotemstr on Monday January 24, 2022 @06:57AM (#30056530)
> Using pointers is terrible because now you have to add a check for null inside your function, as its API contract says "I can accept null values", while references make it clear that it does not make sense to call if you don't have a correct object to pass it.

You need no such NULL check. Specify as part of the function's contract that a pointer be non-NULL: if a caller breaks the contract, behavior is undefined. An unexpected NULL is just like any other violation of the function's contract: a programming error.

Spell out the contract in the function's documentation. A contract is more than the signature. There is absolutely no need for a runtime check (certain special cases aside) to enforce function contracts.

In some cases, assert()ing that a parameter that is expected to be non-NULL is in fact non-NULL can be useful --- but you don't need such checks for correctness of the program, and use of assertions in general ought to be a matter of taste and discretion.

Knee-jerk, automatic checking by random internal functions of a module of pointer parameters for NULL in every single place suggests that the author doesn't fully comprehend the concept of interface contracts generally.

Re:Const all the things? by nemetroid on Monday January 24, 2022 @07:07AM (#30056628)
It's wildly useful to have interface contracts that can be verified by the compiler. In my experience this benefit of using pointers (vs references) only when the argument is optional way outweighs the drawback of not seeing an '&' at the call site.
Re:Const all the things? by quotemstr on Monday January 24, 2022 @07:10AM (#30056666)
> In my experience this benefit of using pointers (vs references) only when the argument is optional way outweighs the drawback of not seeing an '&' at the call site.

Maybe so. Reasonable people differ on this subject.

My point is different: it's just not true that if a parameter is a pointer, then as a general requirement you have to check at runtime whether that pointer is non-NULL. It drives me nuts when I see these checks sprinkled throughout a codebase.

Re:Const all the things? by nemetroid on Monday January 24, 2022 @07:20AM (#30056756)
> My point is different: it's just not true that if a parameter is a pointer, then as a general requirement you have to check at runtime whether that pointer is non-NULL. It drives me nuts when I see these checks sprinkled throughout a codebase.

My point is that if you only ever use pointer arguments to indicate that the argument is optional, then the checks are required.

Re:Const all the things? by stevenhuang on Monday January 24, 2022 @08:41PM (#30066791)
But it is a reasonable rule, hence it being a general requirement in a lot of places.

More nuanced, it's more important to have these null checks on a library's public interfaces than a library's private interfaces. Private interfaces can do with asserts for null check.

Re:Const all the things? by quotemstr on Tuesday January 25, 2022 @03:38PM (#30077691)
> it's more important to have these null checks on a library's public interfaces

No it isn't.

Re:Const all the things? by stevenhuang on Thursday January 27, 2022 @05:34AM (#30098698)
Library authors and users would disagree with you.
Re:Const all the things? by bigbillheck on Monday January 24, 2022 @07:37AM (#30056884)
> Spell out the contract in the function's documentation

The sibling subthread is all about people not wanting to read the function's documentation.

Re:Const all the things? by bluGill on Monday January 24, 2022 @11:21AM (#30059673)
I would love to have compiler enforced contracts. However I'm not sure how to make that happen or even if it is technically possible. The current contracts proposal is a step in the right direction, but just barely a step and I'm not sure if it can go far enough.

Until then I avoid adding undefined behavior to my code for good reasons.