Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"refactor refactor refactor your code." What does this mean exactly and why do it?

Tags:

refactoring

I often heard from professionals blog something like refactoring your code whenever the chance you get. What is it exactly? Rewriting your code in simpler and fewer lines? What is the purpose of doing this?

like image 908
aoghq Avatar asked Nov 13 '09 05:11

aoghq


People also ask

Why do we refactor the code?

The goal of refactoring is to improve internal code by making many small changes without altering the code's external behavior. Computer programmers and software developers refactor code to improve the design, structure and implementation of software. Refactoring improves code readability and reduces complexities.

What refactor meaning?

Refactor definition (writing) To rewrite existing text in order to improve its readability, reusability or structure without intentionally affecting its meaning. Similar to, but sometimes involving more extensive restructuring than, copy editing.

What is refactoring and why is it useful?

But what is Refactoring? -It is a scientific process of taking existing code and improves it while it makes code more readable, understandable, and clean. Also, it becomes very handy to add new features, build large applications and spot & fix bugs. It is a law of nature for fully successful iterative projects.

When should you refactor code?

Code Refactoring Best Practices Make sure to have a backup of your code before doing anything. If you're using Git or another VCS, make sure you're working on your local branch. Verify that you have sufficient test coverage – aiming for 80% or better. Check with your QA/Testing Teams for recommendations or support.


2 Answers

Refactoring code is a process of cleaning up your code, reducing the clutter and improving the readability without causing any side effects or changes to features.

Basically, you refactor by applying a series of code change rules that improve code readability and re-usability, without affecting the logic.

Always unit test before and after refactoring to ensure your logic isn't affected.

like image 114
Andrew Keith Avatar answered Sep 17 '22 15:09

Andrew Keith


This Wikipedia article will give you an idea of the types of things included in the general concept of Refactoring.

The idea is adapt / evolve your code as you go. Simple things may be to rename variables or method parameters, but others may be to pass an additional parameter or to drop one, or to change its type. The data model may evolve as well. etc.

Often refactoring, works hand-in-hand with unit-testing, whereby the risk of "breaking something" is offset by the fact that such an issue may likely be discovered by the automatic testing (provide a good coverage and relevant test cases...).

In a nutshell, the ability to refactor (and btw, most IDE or add-ons to the IDEs, offer various tools that make refactoring easier and less error prone) allows one to write more quickly without stressing about some decisions ("should this object include an array or a list etc...) letting the programmer change some of these decisions as times goes, and with the added insight offered by having a workable, if not perfect solution. See a related concept: agile development.

Beware, refactoring doesn't give you license to start coding without putting any thought in design, in the object model, the APIs etc., however it lessens the stiffness of some of these decisions.

like image 43
mjv Avatar answered Sep 20 '22 15:09

mjv