Extract Function (outside of class) (C++, JS)
In This Article
Moves selected code into a new function within the enclosing namespace.
#Purpose
Extract Function (outside of class) (C++, JS) is great when you need to turn a big, complex method into smaller, simpler ones. Small methods are much easier to maintain and encourage code reuse.
#Availability
Available from the context menu or via shortcuts:
- when one or more statements are selected.
- when an expression is selected.
Using the Intelligent Paste feature:
- When a qualifying code block is cut from a method and pasted outside, it will be automatically converted into a function, and a call to that function will be added to the location the code was cut from.
#Notes
- This refactoring is similar to Extract Method in functionality.
#Example
int TotalPrice(ProductInfo info)
{
bool a = info.count > 10;
if (a)
info.unitPrice = 50;
else
info.unitPrice = 70;
return info.unitPrice * info.count;
}
Result:
int TotalPriceExtracted(ProductInfo &info, bool a)
{
if (a)
info.unitPrice = 50;
else
info.unitPrice = 70;
return info.unitPrice * info.count;
}
int TotalPrice(ProductInfo info)
{
bool a = info.count > 10;
return TotalPriceExtracted(info, a);
}
#Animation
See Also