I just finished reading the chapter "Functions" from Uncle Bob's code. The main advice was to make sure the functions are short - really . And they should only do one thing per level of abstraction. Here is the function from the application that I am doing to learn Cocoa (idea from Andy Matushak ).
- (IBAction)go:(id)sender
{
NSString *output = nil;
if ([[nameInputField stringValue] isEqualToString:@""])
{
output = @"Please enter your name";
}
else
{
NSString *date = [[NSDate date] descriptionWithCalendarFormat:@"%A, %B %d"
timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
output = [NSString stringWithFormat:@"Hello, %@! Today is %@.", [nameInputField stringValue], date];
}
[responseOutputField setStringValue:output];
}
Basically, this function reads a name from a text field ( nameInputField) and displays a message in another text field ( responseOutputField). I am interested in, a) if this function performs "one thing" at the level of abstraction, and b) how to make it shorter.
source
share