Extract Method
Instead of having multiple lines of code with unclear purpose, we extract that code into to well named methods. By extracting these lines of code into methods, we usually give couple of variables clear scope and meaning.
This pattern is also called low-level refactoring. We need to be careful when we use this pattern. Even it is simple to do it, it does not mean we are supposed to do it always.
Rules:
a new methods needs to have a name that is easy to understand
a new method can't use shortcuts or any abbreviations
number of lines in method should be small, but there are some unique exceptions
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler
This pattern is not suggesting we can't have long methods. There are good reasons to have a long method that does one thing well, but maybe it is the nature of that one thing to be long. Here is an example that could be reasonable long method. In this example, lets say we are reading data from a datasource and would rather keep it atomic. I have seen these kinds of methods that were reading data from old mainframes in foreign language and the most readable way to keep them, was to keep that code together.
Example
Lets say we have the following code.
The code gets messy when we start playing with Calendar
class. When we apply Define Method
pattern, we extract all the code to separate unit of functionality.
Last updated
Was this helpful?