Guys! Do you think this is correct about "Code Refactoring"

How correct is this?

Code refactoring is the process of restructuring and optimizing existing code in order to improve its quality and maintainability. It involves making changes to the codebase that do not alter its functionality, but that makes it easier to understand, maintain, and modify.

Code refactoring is important because it helps to prevent technical debt and other issues that can arise when code becomes difficult to understand or maintain. By regularly refactoring code, developers can ensure that their projects remain scalable, reliable, and easy to work on.

Was this insightful? Let me know guys! Let me see those replies

Regularly refactoring code is a bad idea in my view. This should be a rare not regular process.

1 Like

So what would you advice as alternative?

There has to be a specific code base and a specific issue in order for someone to advise refactoring.
So my advise is, don’t give this type of advise unless you have evaluated the code base first.

1 Like

If you’re a development team in charge of creating software, code-refactoring should be the exception, not the norm. Having stringent processes (code styling and docstring guidelines, testing, documentation, peer reviews, etc.) that doesn’t let technical debt arise in the first place must be the norm.

It’s understandable that if a software project was transitioned to you by someone else, you may want to refactor it, perhaps even in phases. But if you are developing something from scratch, it should be the exception as I said.

1 Like

Does Keeping the codebase functional count? or it doesn’t matter if it’s altered.

Why would keeping a codebase functional or not matter in this context?

I’d say the definition you have is correct.

Most of what is said above is taking this definition and applying it to real-world scenarios. This may be the case, but in terms of pure definitions what you gave is what code refactoring is.

Now to consider if there is time, budget and requirements to support refactoring is a whole other story that shouldn’t be considered when approaching the definition. If your codebase is a single line it might not require “regular code refactoring”, but this doesn’t affect the definition of what code refactoring is.

Refactoring, by definition, shouldn’t change how the software works from an end user perspective. If your making refactoring changes and actually breaking functionality, you aren’t really refactoring your just introducing bugs.

If your changing user-facing behavior then its more of a traditional change and not a refactor.

1 Like

In your opinion, would you say that to refactor a code, only two techniques actually work?

  1. Extracting code into smaller, more focused functions or methods and
  2. Removing duplicate code

Is there any other you think I could consider trying?

#project-feedback

I think that is an oversimplification of what refactoring entails. Something as simple as identifier renaming is also refactoring or something as involved as optimization.

Not sure why you are looking for a definition but the exact definition will likely vary depending on who you ask.

1 Like

I need one more help with this.

What are your opinions or suggestions on an automated test I can carry out to ensure my code isn’t broken?

#general

These really feel like homework or essay questions.

2 Likes

Not at all. I m trying to learn from your experiences.

The problem is that someone who wants to know these things would actually tell us what it is they are working on. They wouldn’t ask questions straight out of a homework assignment.

2 Likes

What can i improve on this code

function calculateTotal(items) {
  const factors = {
    food: 1.1,
    default: 1
  }

  return items.reduce((total, item) => {
    const factor = factors[item.type] || factors.default;
    return total + item.price * factor;
  }, 0);
}

const items = [
  { type: "food", price: 10 },
  { type: "clothing", price: 20 },
  { type: "electronics", price: 30 }
];

try {
  console.log(calculateTotal(items));
} catch (error) {
  console.error(error);
}

This code calculates the total price of a list of items, with a 10% surcharge added to the price of any item that is classified as “food”.

take a look on what i have been working on

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

what programming language is it?

Javascript

It’s written in javascript