Beneficially Relating Teensy-tiny Elements

It's All Design

Consider a function that looks like this:

return …big long expression… *
…another big long expression…

Let’s say we replace it by:

width = …big long expression…
height = …another big long expression…
return width * height

From a refactoring perspective we have executed Extract Local Variable twice. But why?

From a Tidy First? perspective we have introduced 2 new elements, the local variables. The return statement is related to the variables by using them. The benefits of the relationship are:

  • The return statement is shorter.

  • The return statement is easier to read for intent.

  • The 2 sub-expressions are easier to read in isolation from each other (assuming they are isolated).

So yes we have more elements, but we also have more benefit. In this case the tradeoff seems clear. In others it will depend on, well, a whole bunch of stuff which TF? will discuss at length.