What is that technique called when you take values in the code that are hardcoded and make them variable, possibly constructing elaborate OO constructs to make that variable properly configurable?
For example you are printing labels, and you are specifying a value of labels to print per page, like say 4
.
Later you realize you have different label sizes, and shapes. You construct a label factory, which calls proper label files, which also configure themselves and know their own size in relation to the page which is then returned via a function call using polymorphism, and that value is then used instead of the hardcoded 4
.
In my own words I can describe it as
Refactoring in effort to increase configurability and flexibility of the software.
but as far as naming specific technique, no..
1
Often among my friends we would call this softcoding. It’s a play on words to imply the opposite of hardcoding and has the nice advantage that it’s recognizable for most programmers.
More technically, if I were to describe it in documentation it would simply be “making the software more configurable” or variations of such.
In math related software I sometimes hear it being called parameterizing the code. Or parameterization. It’s the name of the process of making equations able to change behavior by changing variables (parametric equations).
1
The antipattern is usually called magic numbers, but the refactoring doesn’t have quite such a well-known name. My IDE calls the process of turning literals into parameters externalizing (e.g. “Externalize Strings”).
2
Martin Fowler (author of the book “Refactoring”) would probably do this in more than one step. These steps could be, for example, Replace Magic Number with Symbolic Constant” to replace 4
by a symbol LabelsPerPage
, then Add Parameter to replace the constant by a parameter of the method where it is used. Later, Replace Parameter with Method could be the first step to provide the labels per page through a method which can be made polymorphic afterwards through Replace Type Code With Polymorphism.
(see http://www.refactoring.com/catalog/)
I wouldn’t call this refactoring, as it adds functionality (you can now have other numbers of labels per page). Refactoring is making changes to the code without changing the functionality.
What you are doing is adding a layer of abstraction. Maybe multiple layers, it depends on how you like to count these things — the Label classes and their machinery are a clear new layer of abstraction, but is a LabelFactory its own layer? It doesn’t really matter.