As I showed in previous post it is useful to use Inline Method when we have a lot of functionality which is on several methods and need to put this functionality to the one method.
In this post I want to show, in my point of view, subtechnique of Inline Method which is called Inline Temp.
Why do we need another technique? As I said in my point of view it is not another technique it is subtechnique of Inline Method.
Here I will use simple example, as usual
/**
* This example shows how to use "Inline Temp" technique
* @author The Developer's Info
*
*/
public class InlineTempExample {
public static void main(String[] args) {
String s = getStringSum();
System.out.println(s);
}
/**
* Method returns sum of 10+10 in String representation
* @return String representation of sum
*/
private static String getStringSum() {
int value = 10 + 10;
return Integer.toString(value);
}
}
For this refactoring I need to remove String s from main method
/**
* This example shows how to use "Inline Temp" technique
* @author The Developer's Info
*
*/
public class InlineTempExample {
public static void main(String[] args) {
System.out.println(getStringSum());
}
/**
* Method returns sum of 10+10 in String representation
* @return String representation of sum
*/
private static String getStringSum() {
int value = 10 + 10;
return Integer.toString(value);
}
}
As you can see it is very simple.
Pay attention: in real-world application you can skip this technique for local variables, but if these local variables don’t allow you to do other type of refactoring then you can use Inline Temp technique.
If you’d like to get the latest posts as soon as they’re published, subscribe to website’s feed!

Recent Comments