DEV Community

Discussion on: Refactoring old code

Collapse
 
jvdassen profile image
Jan von der Assen • Edited

I like the way you refactored the old snippet.
However, I think it is often important to consider during refactoring what is being improved. The performance? Testability? Readability?

I have a bad feeling about just refactoring because new methods exist or because the number of LOC is being reduced.
For example, I think the readability is improved by how you refactored the code.

Personally I would go even further and extract the lambda functions. IMO this improves readability, reusability and testability of the considered module. But I'm sure there are plenty of people who don't like it:

const newOutput = makeIterable(data)
  .map(toTriple)
  .sort(byFirstKeyDesc)

function makeIterable(data) { return Object.entries(data) }

function toTriple (pair, index) {
  const defaultColor = '#fff'
  return [ ..pair, colors[index] || defaultColor ]
}

function byFirstKeyDesc (a, b) {
  return b[1] - a[1]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dailydevtips1 profile image
Chris Bongers

Hey/Hoi Jan,

The main goal here was readability, I had to look at the old code for a good minute to understand what was happening.

Might be because the new declarative methods are just more of a "naming" thing to me.

Anyhow,
I really like your way, it makes sense to extract the function, might also improve them being re-used by other code, so valid point!

As for the article,
I'm considering renaming it to "Modernizing code" because the refactor is a bit obsolete, other than readability it doesn't change anything.

Collapse
 
jvdassen profile image
Jan von der Assen • Edited

The main goal here was readability

I think you already greatly improved the codebase. And also generally speaking, stressing the importance of refactoring is a very good point to be made in general!

Agreed, that title would nicely show your intent!

Collapse
 
myleftshoe profile image
myleftshoe

Extracting the lambda functions using meaningful and descriptive function names makes the intention clear - even more readily "grokable" and readable