.NET Tools
Essential productivity kit for .NET and game developers
How-To's
Generalizing the algorithm
One of the common tasks developers face on a regular basis is generalizing an algorithm. This How-To describes how a combination of ‘Extract Method’ and ‘Push Members Down’ refactorings can help you streamline this task.
Suppose that you have some code for calculating yearly bonuses for your customers:
Now you want to award bonus points differently for private and corporate customers. ReSharper to the rescue!
- Extract Method from
o.BillingAmount / 100
: select it, then press Ctrl + Alt + M (alternatively, you can go to the ‘Refactor This’ menu by pressing Ctrl + Shift + R and then select ‘Extract Method’:
In the Extract Method dialog, provide the name of the new method and make sure to make it non-static and not private:
After you click Continue, the code looks like the following:
- Push Members Down on
GetBonusForOrder()
:
In the Push Members Down dialog, check the method and make sure to select its ‘Make Abstract’ checkbox:
After you click Continue, the code looks like the following:
So, you’ve got an abstractGetBonusForOrder
inCustomer
class and two overrides inCorporateCustomer
andPrivateCustomer
(which are identical as yet). Modify their implementations as necessary, and voila!
Read more about the features discussed in this How-To:
Refactoring