September 2017
Beginner to intermediate
384 pages
8h 4m
English
A method is long when it is identified to have multiple responsibilities. Naturally, a method that has more than 20-25 lines of code tends to have more than one responsibility. Having said that, a method with more lines of code is longer. This doesn't mean a method with less than 25 lines of code isn't longer. Take a look at the following code snippet:
void Employee::validateAndSave( ) { if ( ( street != "" ) && ( city != "" ) ) saveEmployeeDetails();}
Clearly, the preceding method has multiple responsibilities; that is, it seems to validate and save the details. While validating before saving isn't wrong, the same method shouldn't do both. So the preceding method can be refactored into two smaller methods that have one single ...