Symmetry
At first, this was all there was:
protocol DisplaysDateRange {
func showDateRange(dateRange: DateRangeData)
}
class DateRangeViewController: NSViewController, DisplaysDateRange {
@IBOutlet var dateRangeButton: NSButton!
func showDateRange(dateRange: DateRangeData) {
dateRangeButton.title = dateRange.range
}
}
Then I implemented a sub-view controller and delegated to it, too:
class DateRangePickerController: NSViewController, DisplaysDateRange {
// ...
}
class DateRangeViewController: NSViewController, DisplaysDateRange {
@IBOutlet var dateRangeButton: NSButton!
@IBOutlet var dateRangePickerController: DateRangePickerController!
func showDateRange(dateRange: DateRangeData) {
dateRangeButton.title = dateRange.range
dateRangePickerController.showDateRange(dateRange)
}
}
Then, by habit, I remembered Kent Beck’s advise to keep symmetry high for clean and maintainable code:
class DateRangeButton: NSButton, DisplaysDateRange {
func showDateRange(dateRange: DateRangeData) {
title = dateRange.range
}
}
class DateRangeViewController: NSViewController, DisplaysDateRange {
@IBOutlet var dateRangeButton: DateRangeButton!
@IBOutlet var dateRangePickerController: DateRangePickerController!
func showDateRange(dateRange: DateRangeData) {
dateRangeButton.showDateRange(dateRange)
dateRangePickerController.showDateRange(dateRange)
}
}
Sure, now there’s a subclass of NSButton
that doesn’t do much and will not be reused in the app. But the intent of delegating DateRangeData
to the sub-components is clearer.
Clean and maintainable code will help you see what’s going on when you read the code. title = dateRange.range
doesn’t convey everything showDateRange(dateRange)
does.