Refactoring Code – Replacing Temporary Variables with Queries

clean codedesignrefactoring

Before I ask my question, I want to note that this is not my own code; it comes from the Refactoring Guru website:

enter image description here

I often find myself using the code on the left, and I have one primary motivation for doing so: in the code on the right, you are unnecessarily pushing and popping a stack frame for the same operation twice in a row. For example, suppose that the base price is in fact greater than 1000. In testing this conditional, you already computed what the base price is… So by the time you get to the body of that conditional, you already know the value, and there's really no point in re-computing it with a second call to basePrice(). In either case—when the base price exceeds 1000 or is less than or equal to 1000—you end up with an extra redundant call to basePrice().

Question: Does the code on the right offer any real advantages over the one on the left? The site mentions that this allows for greater reusability of the code in case other methods want to compute the base price as well, which I agree with.

Best Answer

A refactoring is not a best practice.

A refactoring is a way to change code without changing externally visible behavior. A single well written unit test should be able to pass both the left code and the right code without being touched.

You're reading this post as if the code on the left is always a problem. It's not. Sometimes the code on the right is the problem. This is telling you something you can do if you see the code on the left as a problem. That does not guarantee that the code on the right won't also have problems.

in the code on the right, you are unnecessarily pushing and popping a stack frame for the same operation twice in a row.

You don't actually know that.

It looks like it should call it twice but depending on how it's written and the compiler/interpreter used, it might be optimized away.

Even if it's not, unless the calculation is truly significant, the performance improvement that caching would give you is not worth worrying about.

Some may argue that the code on the left is clearer. Some may argue that the code on the right is doing fewer things and so is better focused on doing one thing.

I'll argue that both of them could be improved by externalizing the dependency on base price and making it an explicit parameter to pass in. Which is yet another refactoring.

Doing that is objectively a refactoring. The idea that it's better is subjective opinion. When making subjective choices ask your team. They're the ones that have to live with it.

Related Topic