News for hackers. Stuff that matters.
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.