Refactoring code

A recent post inspired me to look through my code to see how many times I used an array of objects with find() when I could have just used a simple object for a lookup. I was surprised to discover the many times I did that.

Comparatively, a simple object is more performant than an array of objects, and the corresponding lookup code is cleaner. So, now that I’m tuned into that, my code should be better going forward. Well, you know, relatively. :slight_smile:

Do you refactor regularly? What code snippets have you found that you later replaced with something better?

2 Likes

Hi @dhess

Unless its a project I’m working on or the performance is an issue, I tend to keep changes to a minimum.

If there’s a much better way to do something, then I’ll update the code.

I’ll use for loops for when lots of things are going on.
When it’s straight forward, then methods like .map(), .forEach(), .reduce().

In the beginning I used to spend lots of time optimising code. But as I progressed, I’d learn much better ways later on, so my optimisations took a lot of time, but didn’t really deliver that much benefit at that early stage.

3 Likes

I don’t refactor for performance much. Unless maybe it’s a large data set or something I anticipate using a lot of resources - or most likely - when something becomes a problem.

I do refactor for readability, though. Spent plenty of time coming up with better variable names and changing it across a codebase - or extracting chunks of code into smaller components.

Sometimes coming up with good variable names is the most challenging part of coding, but they make all the difference when you revisit your code later on.

Generally speaking, I’ll refactor as I go. I don’t deliberately go out looking for ugly code to improve, since ugly, working, stable code isn’t hurting anyone.

However, if I need to change something and I haven’t a clue how it works, that’s when I tend to refactor. Refactoring helps me understand the code, so it makes it easier to implement whatever change I needed to make.

1 Like

That makes sense.

I like to refactor code I’ve saved from the fCC challenges when I learn a better way to do something. I also add comments to help me understand concepts that I grappled with, like currying, so that when I go back and review the code, I can understand what it’s doing.

In my original post, I was excited to realize that I can often use a simple object instead of my usual array of objects approach. For example, in one of the daily challenges, the objective was to find the organization name from an acronym. I originally coded it like this:

function findOrg(acronym) {
  const organizations = [{acronym: "NASA", organization: "National Avocado Storage Authority"}, {acronym: "CIA", organization: "Cats Infiltration Agency"}, {acronym: "FBI", organization: "Fluffy Beanbag Inspectors"}, {acronym: "DOJ", organization: "Department Of Jelly"}, {acronym: "WHO", organization: "Wild Honey Organization"}, {acronym: "EPA", organization: "Eating Pancakes Administration"}];
  const obj = organizations.find(org => org.acronym === acronym);
  return obj.organization;
}

then refactored to this:

function findOrg(acronym) {
  const organizations = {"NASA": "National Avocado Storage Authority", "CIA": "Cats Infiltration Agency","FBI": "Fluffy Beanbag Inspectors","DOJ": "Department Of Jelly","WHO": "Wild Honey Organization","EPA": "Eating Pancakes Administration"};
  return organizations[acronym];
}

So much cleaner. And now I’ll be more tuned in when I see an opportunity to use a simple object rather than an array of objects.

2 Likes

That is nice. It’s a good feeling when you find a small tweak, like you did going from an array to object, and it’s just obviously better.

Your second version’s so much better: it’s much easier to read and no longer throws on unknown acronyms (which is probably what you want!)

Good catch! The first version would throw an error if the acronym wasn’t in one of the array’s objects, so the return should have been refactored to: return obj ? obj.organization : undefined;

1 Like