CODE REFACTORING

CODE REFACTORING

Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. 

- Martin Fowler in Refactoring: Improving The Design Of Existing Code

 

Code refactoring is the process of restructuring existing computer code; changing the functions/methods without changing its external behavior. Refactoring improves the code readability, and reduces code complexity. These can improve source code maintainability and extensibility. Refactoring is also a way of designing systems, it eliminates the need for rigorous up-front design, as the process can reveal more options.

Refactoring is typically done in small steps. After each small step, the internal code can be changed significantly, but the system functionality will be unchanged. Programmers typically fix bugs, rearrange methods, eliminate duplicate methods, and do feature additions between these steps. The main goal is that it becomes easier to rearrange the code and change functionality when you have clean code.

Let’s see an example.

Before refactoring:

void printOwing() {

printBanner(); //print details

System.out.println("name: " + _name);

System.out.println("amount " + getOutstanding());

}

After refactoring:

void printOwing() {

printBanner();

printDetails (getOutstanding());

}

void printDetails (double outstanding) {

System.out.println("name: " + _name);

System.out.println("amount " + outstanding);

}

Generally as soon as you notice a problem, it's a good time to refactor the code. Refactors don't have to be giant, time-consuming tasks. For little problems a refactor might be as simple as renaming a variable in a few places. If a problem is left in code for too long, it tends to grow exponentially until the development team spends more time fixing bugs than working on features, and nobody likes just fixing bugs. Making the code readable and maintainable is a tough job but it is highly recommended for software extensibility, for quick bug fixes and saving time in the long run.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

- Martin Fowler in Refactoring: Improving The Design Of Existing Code

It is much more important to write code that will be easy to understand by other software developers than just understood by a computer. A few things that attribute to bad design or unreadable code are duplication and long methods. Duplication makes it harder to change the program, long methods (more than 10 lines is long) is highly discouraged in the clean code philosophy. It is highly recommended that a method should do one thing.

On the other hand when the code change will affect too many things, such that it may render application unusable or you don’t have unit tests to support your changes, it is highly recommended not to refactor code, but may be consider to move on or start fresh.

In summary, to reap the benefits of refactoring it is important to know how
much to refactor a piece of code, when to refactor and when not to.

References: 

To view or add a comment, sign in

More articles by MAHFUZ AFTAB

  • $q service in AngularJS

    $q service in AngularJS

    The Promise object is used for asynchronous computations. A promise represents an operation that hasn't completed yet…

    2 Comments
  • React Image Slider

    React Image Slider

    This article describes the implementation of a simple image slider component with React.js.

    2 Comments
  • Mongoose Aggregate

    Mongoose Aggregate

    Mongoose is an object document modeling (ODM) system that bridges Node.js and MongoDB, enabling elegant data modeling…

  • MACHINE LEARNING

    MACHINE LEARNING

    One great area of technology which is helping to improve the several services that we can use on our smartphones and on…

  • Potentials of Web Based Games in Child Education

    Potentials of Web Based Games in Child Education

    It is generally accepted now that the Internet and online games provide a tremendous opportunity for new forms of…

Insights from the community

Others also viewed

Explore topics