Tuesday 28 February 2012

Code Refactoring

I am dedicating this article to a list of refactoring techinques in addition to the highly cited Martin Fowler's book on Code Refactoring.

Replace Parameter with a New Method (Specialize Method)


Chapter "Replace Parameter with Method" describes when and how to remove passing an argument if method can retrieve argument's value. But I came across to a slightly different case - when some method is frequently called with an argument that it can retrieve:

Let class CConfig be defined as:



Let's assume that the following snippet occurs very often throughout the code:



LoadOption() is obviously able to retrieve m_nOptionSelected so why having two calls instead of the one - of the new method - LoadSelectedOption():



Now you can make a single method call, without a need to provide parameters:


No comments: