Join Simon Allardice for an in-depth discussion in this video The Inline Method refactoring, part of Foundations of Programming: Refactoring Code.
While Extract method is one of the most common area factorings you'll ever use. We will also, occasionally, encounter the need for its exact opposite, the in-line method refactoring. Literally, finding a call to a method that adds no value, no additional modularity, no additional kyote/g, it's simply not worth it. So in this straightforward example I have a method here that's calling the method get value of PI located somewhere else in our code. And all that does is return Math.PI, typical way to get PI in Java. Now refactoring is sometimes accused of extracting everything it possibly can into its own tiny method.
This one really is pointless. So to do this refactoring, it's nothing more than taking the body of the method, in this case Math.PI. And just directly using that in every location that method was originally being called. And then Deleting the method, test and verify. The one thing to watch out for is that the simple method you're intending to get rid of is polymorphic, in that it may have been written in a very simple format in the super class just so it could be overwritten in a subclass.
And if that is the case, you don't do this refactoring, you just leave it alone. You can't overwrite a method that doesn't exist. Essentially if there's any additional complexity, any, yes, but, or this just doesn't really work exactly this way, then you just don't do it. The entire point of inline method, is that the existing method call added no benefit whatsoever. The only other occasional use for reason for in-lined method kind of informally. Is in the case where you have a bunch of original methods that are some what randomly and badly written.
So you have method 1, method 2, method 3, method 4. In this case you might temporarily use in-lined method to bring them all in line make sense of them. And then use extract method to break them back apart into better refactor code.
- What is refactoring?
- Recognizing common code smells
- Simplifying method calls
- Making conditions easier to read
- Using refactorings at the method, class, and application level