1

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
  • The term I most often hear is unhardcoding. Or, in Russian, расхардкодь эту хуйню.
    – sbichenko
    May 6, 2015 at 7:26

4 Answers 4

4

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
  • Personally, I find "parameterize" to be quite intuitive, although I often end up say something like "allow X to be changed" in the commit logs nonetheless.
    – Rufflewind
    May 6, 2015 at 6:38
2

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
  • 4
    I suggest "muggle-fying the numbers" or "un-hogwartising the numerals". They will also help to identify the members of your team who actually read the commit messages.
    – null
    May 5, 2015 at 14:53
  • 1
    Since removing literals is about increasing DRY-ness, maybe we should call it "towelling"? (Also, Douglas Adams reference.) May 5, 2015 at 15:21
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/)

2

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.