News for hackers. Stuff that matters.
Re:Avoid Null Checks by Replacing Finders with Tellers by tbrownaw on Monday June 17, 2013 @01:00PM (#5894311)
Why? What is so bad about having your conditionals be explicit?

What happens when you need an "else" branch?

Is not having a person for the given ID an error, is it a "do nothing", or does it mean to create a new person?

What does the callback version do if you forget to make your index unique, and end up with multiple people with the same ID; does it get run multiple times? The version that returns a Maybe<Person> at least has an obvious proper response (throw an error).

Re:Avoid Null Checks by Replacing Finders with Tellers by wpietri on Monday June 17, 2013 @03:01PM (#5895188)
We make something explicit when we want to call attention to it. Otherwise, we should make it implicit.

Attention is a limited commodity, and I think it's worth preserving for the important things. Sometimes it really is worth reminding people, "Hey, this could be null!" But a lot of the time that is boring.

To me this often comes back to favoring meaning over mechanism. The higher up I go in code, the more I want it to be about what the system means, not the particular mechanism I've chosen to make it work. E.g., Computers may be implemented via impure silicon, but most of the time we can forget that. I try to apply that to any sort of mechanism.

Re:Avoid Null Checks by Replacing Finders with Tellers by masklinn on Monday June 17, 2013 @01:35PM (#5894563)
> What happens when you need an "else" branch?

Then you use one, but null checks generally aren't cases of if/else conditionals, they're more along the lines of "either I have one or mayday mayday". Sometimes slightly softened into "either I have one or use this default thing"

Re:Avoid Null Checks by Replacing Finders with Tellers by tbrownaw on Monday June 17, 2013 @02:09PM (#5894816)
more along the lines of "either I have one or mayday mayday".

That would be the Perl way of

    my $person = $data_source->get_person($id) or die "No person available";
ie, there's code to be executed (to raise the error) in the case that there is no person. The callback structure from the article doesn't allow for this.
Re:Avoid Null Checks by Replacing Finders with Tellers by InclinedPlane on Monday June 17, 2013 @03:38PM (#5895396)
Because once it's possible for an object to be null defensive programming means that you need to check damned near everywhere. After every method call, for example. And that's very, very cumbersome to maintain and very non-DRY.
Re:Avoid Null Checks by Replacing Finders with Tellers by thedufer on Monday June 17, 2013 @02:57PM (#5895166)
Coming from working with node.js, this feels very strange. How do I call a callback in the else case? It looks nice and all, but I don't see a way to make this work if db calls are asynchronous.

Any ideas?

Re:Avoid Null Checks by Replacing Finders with Tellers by anonymoushn on Monday June 17, 2013 @03:29PM (#5895351)
Pass two callbacks, or make your callback begin with if(err). Which looks suspiciously like a null check.
Re:Avoid Null Checks by Replacing Finders with Tellers by anonymoushn on Monday June 17, 2013 @03:34PM (#5895377)
Why? Because people won't write null checks some of the time even if they need to. Of course, this solution to that problem introduces a bunch of other problems that you've pointed out, and it would be much better for one's sanity to just use an option/maybe.
Re:Avoid Null Checks by Replacing Finders with Tellers by demallien on Monday June 17, 2013 @04:22PM (#5895689)
I tend to consider "if" statements to be a bit like "goto" statements - that is, they can sometimes be useful, but mostly the intent of your code will be clearer if you can avoid them. Use with parsimony.