c# - Avoid Null Checking by using lambdas -
in article avoid null checks replacing finders tellers author gives ruby example avoid null checking, if object returned block run, if not isn't.
data_source.person(id) |person| person.phone_number = phone_number data_source.update_person person end
i'd same thing in c# using lambda function having trouble coming example same type of thing. create object factory accept id number , lambda function?
well don't know ruby , don't understand exact example given, suspect like:
datasource.update(id, person => person.phonenumber = phonenumber);
where datasource.update
would:
- have signature of
void update(string id, action<person> updateaction
(or possibly returnbool
indicate whether or not found person) - be implemented as:
- find person given id
- if doesn't exist, return immediately
- otherwise, execute given action, , update backing store modified object
or more (and closer original ruby):
datasource.withperson(id, person => { person.phonenumber = phonenumber; datasource.updateperson(person); };
personally prefer first form: it's more specific it's trying achieve, may lend better implementation, , it's cleaner in calling code.
Comments
Post a Comment