News for hackers. Stuff that matters.
Re:Why we've banned null in our codebase by ajross on Friday January 04, 2013 @11:47AM (#5008417)
This seems to be missing the point. There certainly are paradigms (C++ RAII being the most obvious) where you work with objects or data that can be affirmatively guaranteed to exist. And those indeed have value and are worth emulating even in environments that don't natively support them (the linked article is in Java).

But that has nothing to do with "null". There are other paradigms (linked lists or weak references, say) where you want the ability to distinguish between a "valid" object and an invalid/empty/not-present one. And whether you call this thing "null" or not is just a semantic question. It's no less an error to operate past the end of the list or try to access an expired cache empty just because you decided to allocate memory to store it.

Re:Why we've banned null in our codebase by noelwelsh on Friday January 04, 2013 @11:56AM (#5008459)
Nah, that's not the issue. The way the game works is this:

1. Decide on some property you want the compiler to enforce for you. E.g. I care about only operating on values that exist.

2. Encode this in the type system. E.g. use the Option type to distinguish those values that may not exist (Options) from those that definitely do (everything else).

It's not about being able to distinguish valid and invalid values, it about getting the compiler to enforce it for you. This is the real power of modern type systems -- not bookkeeping to keep the optimizer happy but getting the computer to check the things you care about.

Re:Why we've banned null in our codebase by NinetyNine on Friday January 04, 2013 @12:05PM (#5008515)
In the specific example of the blog post, an isDefined() is still necessary. How is `isDefined()` different than `ref !== null`?
Re:Why we've banned null in our codebase by lmm on Friday January 04, 2013 @12:23PM (#5008611)
The difference is that you can distinguish at the type level between values that could be "null" (Optionals) and values that are never null, and the two look different from each other. Otherwise you tend to have one of two problems:

a) Every function checks all its parameters for null, and you do null checks before calling a method on any object, wasting time.

b) It's down to the programmer to figure out where a null check is necessary and where it isn't, and they sometimes get it wrong.

(Of course, better code will avoid calling isDefined or get, but even if you just use it as a like-for-like replacement, using optionals in place of null can improve your program)

Re:Why we've banned null in our codebase by michaelt on Friday January 04, 2013 @12:57PM (#5008793)
Null checks in java are quite cheap, from what I've read.

If your code doesn't check for a null, JIT adds in an implicit check in case it needs to throw a NullPointerException. When you put a check in your code, it knows to eliminate the implicit check.

Putting null checks everywhere does lead to ugly, complicated-looking code though; it'd be nice if there were a simple syntax for non-null parameters.

Re:Why we've banned null in our codebase by bunderbunder on Friday January 04, 2013 @02:05PM (#5009155)
It's not really about computational expense. It's about making reliable, maintainable software.

The problem with null is that it's a semantic gremlin. Depending on context it could mean all sorts of things including but not limited to, "the value has not yet been set", "the value is undefined", "the value is nothing" or "the software is broken". Which one of those is the correct interpretation at any given time is a question that can only really be answered by a programmer with a good knowledge of the codebase.

Worse yet, even in cases where null isn't a desired behavior it's still inescapable. If your procedure accepts reference types as parameters, it doesn't matter if it has no interest in taking null as an argument, or that it doesn't even make any sense (semantically) for null to be passed as an argument. It still has to be prepared to be handed null as an argument. At a language level, there's simply no support for the concept of, "Guys, I really do need something to go here." Which is simply insane, considering how simple a concept it is.

In languages that don't have null references, it's not that the various semantic meanings of null have been thrown out. All that's been removed is the ambiguity: For each possible (desirable) meaning, a construct is provided to represent specifically that meaning. Meaning that you've gotten rid of the problem of programmers having to magically know what null means in that case, and the worse problem of programmers introducing bugs resulting from them failing to understand null's meaning in a particular context.

Re:Why we've banned null in our codebase by anonymoushn on Friday January 04, 2013 @12:14PM (#5008562)
It isn't. The difference is that in all the places where you don't use Option[T], you no longer have to have a null check because you have "banned null."

This doesn't seem like a particularly good idea if any of your code needs to run reasonably quickly. Even relatively tame uses of Option[T] in the collections that ship with scala can have a terrible impact if you want to run the code rather than just typecheck it. For instance, if you have some loop in which you want to perform I/O and call getOrElseUpdate on your scala.mutable.HashMap[T, Long], your code will probably spend a majority of its time creating millions of java.lang.Longs, stuffing each one into its own newly-minted Some[Long], and GCing these two things.

Re:Why we've banned null in our codebase by noelwelsh on Friday January 04, 2013 @12:53PM (#5008772)
If you changed reasonably quickly to extremely quickly I would agree. There is some overhead involved but it is very small. In a copying collector, like the 1st generation of Hotspot's generational GC, the GC cost is proportional to the amount of non-garbage you allocate. Creating lots of very short-lived objects is almost free (allocation is very fast in all GCs). The cost is in memory consumption -- a classic tradeoff of time for space.

Of course if this is an issue you can always fall back to Java's collections or a collection class specialised to Longs. I think the Scala implementors made the right tradeoff for a general purpose library.

Re:Why we've banned null in our codebase by mseebach on Friday January 04, 2013 @12:15PM (#5008566)
The idea is that you don't want to call isDefined at all - you'll want to call getOr(T default) or pass it through a chain of methods that are overloaded doFoo(Some<T> x) { .. stuff .. return Some<U> }/doFoo(None x) { return None; }
Re:Why we've banned null in our codebase by anonymoushn on Friday January 04, 2013 @12:25PM (#5008623)
The latter won't work for you as-is:

  scala> def doFoo(x: Some[Int]) = x.get*2
  doFoo: (x: Some[Int])Int

  scala> def doFoo(x: None.type) = None
  doFoo: (x: None.type)None.type

  scala> doFoo(Option(3))
  <console>:9: error: type mismatch;
   found   : Option[Int]
   required: None.type
                doFoo(Option(3))
                            ^
But it will work as either of these:

  scala> def doFoo(x: Option[Int]): Option[Int] = x match {
       |   case Some(n) => Some(n*2)
       |   case _ => None
       | }

  scala> def doFoo(x: Option[Int]) = x.map(_*2)
  doFoo: (x: Option[Int])Option[Int]
Re:Why we've banned null in our codebase by ajross on Friday January 04, 2013 @12:01PM (#5008493)
How is you description different from "emulating paradigms like RAII in environments that don't natively support them?". And it doesn't address things like the trivial list/cache case, where clearly you need a null(-equivalent object reference). Clearly the compiler isn't going to be able to enforce that all lists are infinitely long...

I'm not saying it's a bad trick. I'm saying that thinking of it as "banning null" is an incomplete understanding of the issue.

Re:Why we've banned null in our codebase by fusiongyro on Friday January 04, 2013 @12:08PM (#5008533)
I hate to say "you should," but you really should spend a few hours going through a Haskell tutorial. You'll be surprised how different things are.

The Haskell equivalent to this Scala Option fiasco is Maybe, which is defined as:

data Maybe a = Just a | Nothing

"Nothing" is not a null reference and "Just a" is not an object with a pointer to another object. "Just a" is a constructor with a single value parameter. "Nothing" is a constructor that takes no parameters. These are values, not references or values with references or references to references. "Nothing" always has a specific type (Maybe Integer, Maybe String, etc.) even though it has no auxiliary data of its own. This means "Nothing" can't show up anywhere you have values--it can only show up where you expect a value of type Maybe. Indeed, the whole point here is that you are forced to explicitly reckon with the possibility of null where it can happen, and you are freed from responsibility to worry about it everywhere else.

Haskell does just fine without the kind of null we're talking about, in a way that I suspect is fundamentally different from what you're intuiting.

Re:Why we've banned null in our codebase by mseebach on Friday January 04, 2013 @12:03PM (#5008508)
When you need a null, you use None.
Re:Why we've banned null in our codebase by anonnona on Friday January 04, 2013 @12:02PM (#5008500)
I think you are missing the point.

The problem is that for the most part needing to check whether an object is null/"valid" should be an exceptional case, not a common one. Most object variables do not need to have the optional/null state. For languages that allow every object to be null you create a situation where you need to have null checks for every object you encounter unless you come up with some error prone convention outside the language itself. Every method/function has to handle the nulls, it's not clear whether an object will be null when passed into a method or not, and if you are writing APIs for people to use, you must check for nulls because you cannot expect much from the user of the apis.

TLDR; Having non null objects by default reduces the amount of code, which in turn increases the correctness of the code.

Re:Why we've banned null in our codebase by barrkel on Friday January 04, 2013 @12:27PM (#5008643)
needing to check whether an object is null/"valid" should be an exceptional case

Handily enough, C#, Java and similar languages actually throw an exception when you try to work with a null reference as if it wasn't null. These exceptions are generally only cryptic when the null references are stored somewhere other than the stack before being manipulated. That is, so long as foo(null) throws an exception before foo() returns, it's no big deal to diagnose, and mostly unnecessary to check the value of the parameter as it passes through the graph of method calls.

(To a degree I'm arguing a devil's advocate position, as I have a lot of sympathy for encoding not-null-by-default into the type system.)

Re:Why we've banned null in our codebase by anonnona on Friday January 04, 2013 @12:41PM (#5008708)
In that world you have bugs lying in wait inside the code ready to rear their head at inopportune times, better to not allow their existence at all. Also in terms of difficulty of fixing bugs its a lot easier to fix bugs at compile time on your machine then it is later on in the release cycle. I think this kind of attitude is the reason why maintenance is the most expensive part of software development. It's also the work involving the most drudgery.
Re:Why we've banned null in our codebase by barrkel on Friday January 04, 2013 @05:27PM (#5010348)
In my experience, the two biggest reasons for expensive maintenance are (a) not all the code is understood by the people maintaining the system because past maintainers have moved on, and (b) assumptions have been made for performance or simplicity reasons that remove abstraction boundaries and embed assumptions about how the whole works inside different parts of the system, such that when one part needs major change, many other parts also need careful change; this is made worse by (a), because later maintainers usually only understand a subset of the individual parts.

This isn't something easily fixed by a compiler or language feature. There's no magic bullet.

Bugs "lying in wait" are a minor problem; if they lie in wait long enough, they are almost by definition not a problem at all.

Re:Why we've banned null in our codebase by cube13 on Friday January 04, 2013 @12:43PM (#5008718)
>The problem is that for the most part needing to check whether an object is null/"valid" should be an exceptional case, not a common one.

It's possible to code around it if you're presenting a packaged executable or product, but if you're creating an API of any sort, or dealing with user input, it should be a standard case.

Blank inputs or null cases should always be checked in that case.

Re:Why we've banned null in our codebase by anonnona on Friday January 04, 2013 @12:59PM (#5008802)
I don't think you understand. If you don't allow null by default in your type system then you don't have to check for it when you don't need it.

Dealing with user input is a separate issue, yes you need to validate it. The problem with every type allowing null is that it infects EVERY object for a situation that isn't always needed or wanted.

Re:Why we've banned null in our codebase by mseebach on Friday January 04, 2013 @12:02PM (#5008499)
No, it's very much not semantic. With nulls, a public method accepting a Foo must be prepared to deal with receiving a null-pointer for Foo. With Options, if you declare a parameter as Foo, you've communicated that you expect a Foo, if you declare a Option<Foo>, you're prepared to accept a None.