Re: Dynamic Method Creation
Re: Dynamic Method Creation
- Subject: Re: Dynamic Method Creation
- From: Ken Thomases <email@hidden>
- Date: Sat, 15 Sep 2012 00:16:25 -0500
On Sep 14, 2012, at 11:38 PM, Steve Steinitz wrote:
> I'm writing a roster window for our point-of-sale app. I have some methods (on Employee) for binding to a view-based TableView (which looks a bit like a week planner), a set of methods for each day of the week. So far I have only implemented and debugged the Monday ones:
>
> - (Shift *) mondayShift {return [self shiftForDayOfWeek: [[self class] mondayName]];}
> - (void) setMondayShift: (Shift *) value_ {[self willChangeValueForKey: @"shift" withWeekdayPrefix: [[self class] mondayName]]; [self didChangeValueForKey: @"shift" withWeekdayPrefix: [[self class] mondayName]];} // KVO hack
> - (NSInteger) mondayStartHourValue {return [self.mondayShift.startHour integerValue];}
> - (void) setMondayStartHourValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"startHour" forWeekday: [[self class] mondayName]];}
> - (NSInteger) mondayStartMinuteValue {return [self.mondayShift.startMinute integerValue];}
> - (void) setMondayStartMinuteValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"startMinute" forWeekday: [[self class] mondayName]];}
> - (NSInteger) mondayEndHourValue {return [self.mondayShift.endHour integerValue];}
> - (void) setMondayEndHourValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"endHour" forWeekday: [[self class] mondayName]];}
> - (NSInteger) mondayEndMinuteValue {return [self.mondayShift.endMinute integerValue];}
> - (void) setMondayEndMinuteValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"endMinute" forWeekday: [[self class] mondayName]];}
> - (NSInteger) mondayBreakDurationValue {return [self.mondayShift.breakDuration integerValue];}
> - (void) setMondayBreakDurationValue: (NSUInteger) value_ {[self setShiftValue: value_ forKey: @"breakDuration" forWeekday: [[self class] mondayName]];}
> - (Shift *) createMondayShift {return [self createShiftForDayOfWeekNamed: [[self class] mondayName]];}
> - (void) clearMondayShift {[self clearShiftForWeekdayNamed: [[self class] mondayName]];}
No offense, but this seems like a terrible design. And your KVO hack method screams of papering over a bug.
The existence of methods like -setShiftValue:forKey:forWeekday: seem problematic. A Shift should be a fully self-sufficient object with proper properties. Therefore, this interface shouldn't be on the Employee class. A Shift should simply have properties like startHour, startMinute, endHour, endMinute, and breakDuration. Setting them should be done on the appropriate Shift object.
Why in the world would you want to duplicate all of those properties on Employee for each day of the week, when you already can just reference the shift? What I mean is, why would you want client code to look like:
[someEmployee setMondayEndMinuteValue:whatever];
when it could be:
someEmployee.mondayShift.endMinute = whatever;
?
And, even then, are you really anticipating writing client code which hard-codes the name of the weekday that it's operating on all over the place? You're going to write code which refers to Monday and then other code which refers to Tuesday, etc.? Surely you're going to be doing the same sorts of things to the shifts of one day that you'll be doing to shifts of the other days. So, the code should be generalized. The weekday to operate on will be represented in _data_, not in code. (And, for what it's worth, I recommend using an enum to represent which day, not a string, which is error-prone.)
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden