News for hackers. Stuff that matters.
Re:Avoid Null Checks by Replacing Finders with Tellers by jlarocco on Monday June 17, 2013 @01:46PM (#5894632)
This is a strange article. The biggest problem with null checks isn't that they make code difficult to read - it's that people forget to add them and that can cause bugs and security vulnerabilities. I've never known anybody to complain that they made code difficult to read. Unless they're mixed in with "real" program logic, like "if (obj != NULL && obj->value()<20)" then they're really not too hard to ignore.

And the author doesn't seem familiar with many programming languages. Using his "finders and tellers" idiom isn't as special as he thinks, and doesn't require lambdas or blocks. Here's a similar idea in Python, not using its lambdas:

    for person in data_source.person(id):
        person.setPhoneNumber(phone_number)
        data_source.update_person(person)

    # And for other situations:
    with open("whatever.txt") as inf:
        for line in inf:
            print(line)
C++ has had similar functionality in the STL using iterators since before C++98.

Lambdas and blocks are convenient and nice features to have, but they're not required for this.

Re:Avoid Null Checks by Replacing Finders with Tellers by untothebreach on Monday June 17, 2013 @01:52PM (#5894690)
It seems like the reason for the author recommending the use of blocks/lambdas is to put the null-checking code in the `data_source` library, rather than user code. That is, if `data_source.getPersonById` doesn't find a person for that id, the block/lambda doesn't get executed at all, and there only had to be a null-check/not-found-check in one place. In your first Python example, both `.setPhoneNumber` and `.update_person` would be executed, and both would have to be smart enough to not persist anything from a "null object" (assuming data_source.person(id) returned a valid `Person` object with null fields).
Re:Avoid Null Checks by Replacing Finders with Tellers by jlarocco on Monday June 17, 2013 @02:15PM (#5894864)
No, data_source.person(id) will return a collection of 0 or more Person objects, and the for loop will iterate over them and call .setPhoneNumber and .update_person methods on each them. The null check is replaced by iteration over a possibly empty collection.

The author's example code appears to be doing the same thing, but iterating using Ruby's block syntax:

    data_source.person(id) do |person|
      person.phone_number = phone_number
      data_source.update_person person
    end
I'm assuming data_source.person(id) would return 0 or more Person objects.
Re:Avoid Null Checks by Replacing Finders with Tellers by dragonwriter on Monday June 17, 2013 @04:11PM (#5895613)
> The author's example code appears to be doing the same thing, but iterating using Ruby's block syntax

Ruby's block syntax is essentially a method of passing an anonymous function as an argument to a method call. It is used by the standard libraries iteration method (e.g., Enumerable#each), but its use here is not iteration (though the teller could be implemented with iteration under the covers.)

data_source.person(...) appears to be a method that takes an id, attempts to retrieve the person with the ID, and, if such a person object exists, yields the person object to the block. From the code, it doesn't look like it is designed to be a collection (it could be implemented on top of a finder that returned a collection, or a finder that returned either a record or nil.)

Re:Avoid Null Checks by Replacing Finders with Tellers by dragonwriter on Monday June 17, 2013 @06:44PM (#5896464)
To illustrate that this is not iteration, assuming the existence of a DataSource class with a private find_person(id) finder that returns either a person object or nil, the method being called here could be implemented thusly:

  class DataSource

    def person(id)
      p = find_person(id)
      p && yield p
    end

  end
Re:Avoid Null Checks by Replacing Finders with Tellers by masklinn on Monday June 17, 2013 @03:57PM (#5895535)
> The author's example code appears to be doing the same thing, but iterating using Ruby's block syntax:

It's not iterating.

Re:Avoid Null Checks by Replacing Finders with Tellers by jlarocco on Monday June 17, 2013 @06:31PM (#5896402)
Does it matter? The null check is gone, so it achieves the author's primary goal.

Conceptually, they both say "update this field for every entry having this ID" and at a quick glance the code for each language looks almost the same.

Re:Avoid Null Checks by Replacing Finders with Tellers by masklinn on Tuesday June 18, 2013 @03:42AM (#5898065)
> Does it matter?

To an extent yes. I'd fully expect the Ruby version to run more code after the block has been executed, the Python version much less so (even though it is possible) for instance.

Re:Avoid Null Checks by Replacing Finders with Tellers by masklinn on Monday June 17, 2013 @01:49PM (#5894665)
The issue here is that the semantics are weird, an iteration block is expected to iterate 0..n times, not 0..1. I know I'd never expect a loop primitive to be used as a conditional.

And the context manager can't handle this case, as it's not able to skip the block body.

Re:Avoid Null Checks by Replacing Finders with Tellers by tbrownaw on Monday June 17, 2013 @02:15PM (#5894858)
I do this in my PL/SQL code occasionally, when I'm feeling lazy and either don't want to handle errors properly or want to take advantage of auto-defined iteration variables.

Since it's a nasty dirty hack that tends to misbehave on bad data instead of failing cleanly, I generally restrict it to experimental/throwaway code. Seeing it recommended as a good idea is a bit WTF.

Re:Avoid Null Checks by Replacing Finders with Tellers by jlarocco on Monday June 17, 2013 @02:21PM (#5894911)
I didn't mean to imply it's a good idea or not. In my own code I'd use the null check and throw an exception. It's suspicious that it's trying to update a non-existent record, so I'd usually want it to break so I can fix the underlying logic bug.

I was simply pointing out that the technique is available in other languages without using lambda and/or blocks.

Re:Avoid Null Checks by Replacing Finders with Tellers by dragonwriter on Monday June 17, 2013 @04:07PM (#5895590)
> Since it's a nasty dirty hack that tends to misbehave on bad data instead of failing cleanly

This assumes that an empty result set of a finder is "bad data"; there are times when this is true (in which case, the basic "teller" idiom as illustrated here isn't a good choice), but there are lots of times where it isn't, too.

Re:Avoid Null Checks by Replacing Finders with Tellers by tbrownaw on Monday June 17, 2013 @04:17PM (#5895662)
There's also the case where I forget a unique index or get the query wrong, and get several records when I'm only expecting one.
Re:Avoid Null Checks by Replacing Finders with Tellers by dragonwriter on Monday June 17, 2013 @04:22PM (#5895692)
The teller strategy can work for those; that's all checking that can be done in the teller (or, in the case of teller-implemented-on-top-of-finder) in the finder. There's no reason to repeat the logic checking for this case all over the application.
Re:Avoid Null Checks by Replacing Finders with Tellers by danneu on Monday June 17, 2013 @02:15PM (#5894865)

    > Many codebases are littered with them [null checks] and, 
    > as a result they [codebases] are often very hard to understand.
I wager they're referring to null checks complicating the codebase (introducing uncertainty into method signatures and argument values down the chain).

And that's what creates a ripe environment for such bugs and vulnerabilities that you mention.

Anyways, my big a-ha moment in programming was when I started thinking in terms of data-structures and mapping functions/transformations across them.

    (map update-phone (filter ... users))