this post was submitted on 05 Sep 2024
45 points (92.5% liked)

Programming

16975 readers
1288 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
all 18 comments
sorted by: hot top controversial new old
[–] [email protected] 11 points 1 week ago* (last edited 1 week ago) (2 children)

The items don't seem concise and always clear. But seems like a good, inspiring resource for things to consider.

If it is expected that a method might fail, then it should fail, either by throwing an Exception or, if not - it should return a special case None/Null type object of the desired class (following the Null Object Pattern), not null itself.

I've never heard of evading null with a Null object. Seems like a bad idea to me. Maybe it could work in some language, but generally I would say prefer result typing. Introducing a result type wrapping or extending the result value type is complexity I would be very evasive to introduce if the language doesn't already support result wrapper/state types.

[–] [email protected] 8 points 1 week ago

We use null objects at work, and as another person said they are a safety feature. Here’s how they work: they are wrappers around another type. They provide the same interface as the wrapped type. They store one global instance of the wrapped type, default initialized, in a memory page marked read-only.

Here’s why they are considered a safety feature (note: most of this is specific to c++).

Suppose you have a collection, and you want to write a function that finds an item in the collection. Find can fail, of course. What do you return in that case? Reasonable options would be a null pointer, or std::nullopt. Having find return a std::optional would be perfect, because that’s the exact use case for it. You either found the item or you did not.

Now, the problem is that in most cases you don’t want to copy the item that was found in the collection when you return it, so you want to return a pointer or a reference. Well, std::optional<T&> is illegal. After all, an optional reference has the same semantics as a pointer, no? This means your find function cannot return an optional, it has to return a pointer with the special sentinel value of nullptr meaning “not found”.

But returning nullptr is dangerous, because if you forget to check the return value and you accidentally dereference it you invoke undefined behavior which in this case will usually crash the program.

Here’s where the null object comes in. You make find just return a reference. If the item is not found, you return a reference to the null object of the relevant type. Because the null object always exists, it’s not UB to access it. And because it is default initialized, trying to get a value from it will just give you the default value for that data member.

Basically it’s a pattern to avoid crashing if tou forget to check for nullptr

[–] [email protected] 2 points 1 week ago (2 children)

I’ve never heard of evading null with a Null object.

This is quite standard, and in fact it's even a safety feature. C++ introduced nullptr defined as an instance of std::nullptr_t explicitly with this in mind.

https://en.cppreference.com/w/cpp/language/nullptr

This approach is also quite basic in monadic types.

[–] [email protected] 4 points 1 week ago* (last edited 1 week ago) (2 children)

with this in mind

With what in mind? Evading NULL?

Languages that make use of references rather than pointers don't have this Dualism. C# has nullable references and nullability analysis, and null as a keyword.

What does your reasoning mean in that context?

[–] [email protected] 4 points 1 week ago* (last edited 1 week ago) (1 children)

Languages that make use of references rather than pointers don’t have this Dualism.

It's not about references vs pointers. You could easily have a language that allowed "null references" (edit: too much C++; of course many languages allow null references, e.g. Javascript) or one that properly separated null pointers out in the type system.

I agree with your point though, using a special Null value is usually worse than using Option or similar. And nullptr_t doesn't help with this at all.

[–] [email protected] 0 points 1 week ago

With what in mind? Evading NULL?

Depends on your perspective. It's convenient to lean on type checking to avoid a whole class of bugs. You can see this either as avoiding NULL or use your type system to flag misuses.

Languages that make use of references rather than pointers don’t have this Dualism. C# has nullable references and nullability analysis, and null as a keyword.

C#'s null keyword matches the monadic approach I mentioned earlier. Nullable types work as a Maybe monad. It's the same concept shoehorned differently due to the different paths taken by these languages.

[–] [email protected] 0 points 1 week ago* (last edited 1 week ago) (1 children)

"Monadic type" has something like three meanings depending on context, and it's not clear which one you mean. One of them is common in math, but not so common in programming, so probably not that. But neither "parametric types with a single argument" nor "types that encode a category-theoretic monad" have the property you say, as far as I know.

I imagine you're probably referring to the latter, since the optional monad exists. That's very different from returning null. The inhabitants of Integer in Java, for example, are the boxed machine ints and null. The inhabitants of Optional[Integer] (it won't let me use angle brackets here) are Optional.of(i) for each machine int i, Optional.empty(), and null.

Optional.empty() is not null and should not be called a "Null object." It's also not of type Integer, so you're not even allowed to return it unless the function type explicitly says so. Writing such function types is pretty uncommon to do in java programs but it's more normal in kotlin. In languages like Haskell, which don't have null at all, this is idiomatic.

[–] [email protected] 0 points 1 week ago (1 children)

I think you're trying too hard to confuse yourself.

[–] [email protected] 7 points 1 week ago (1 children)

This doesn't seem overly useful.

It's a list taken out of a bunch of books with no regard for how something can be the best path in one language and a smell in another language.

Look at this page for example: https://luzkan.github.io/smells/imperative-loops

It suggests using functional loop methods (.map(), .reduce(), .filter()) instead of using imperative loops (for, for in, for each) but completely disregards the facts that imperative loops also have access to the break, continue, and return keywords to improve performance.

For example: If I have an unsorted list of 1000 cars which includes a whole bunch of information per car (e.g. color, year manufactured, etc...), and I want to know if there were any cars were manufactured before the year 1980, I can run an imperative loop through the list and early return true if I find one, and only returning false if I haven't found one by the end of the list.

If the third car was made in 1977, then I have only iterated through 3 cars to find my answer.

But if I were to try this with only functional loops, I would have to iterate through all 1000 cars before I had my answer.

A website with blind rules like this is going to lead to worse code.

[–] [email protected] 4 points 1 week ago (2 children)

..what? At least with Java Streams or Kotlin Sequences, they absolutely abort early with something like .filter().first().

[–] [email protected] 5 points 1 week ago

Same in Python, Rust, Haskell and probably many others.

But apparently JS does work that way, that is its filter always iterates over everything and returns a new array and not some iterator object.

[–] [email protected] 1 points 1 week ago

@mbtrhcs @spartanatreyu well Java Streams try to, but it's not too hard to get them to accidentally process too much, or even blow up completely.

(This isn't a comment on coding styles or the article though)

[–] [email protected] 4 points 1 week ago

This is like SCPs but for devs