Tuesday, 29 March 2016

What is Refactoring ?


Refactoring is a process of changing a software system in such a way that it does not alter external behavior of code yet improves internal structure.
It's a disciplined way to clean up code that minimizes the chances of introducing bugs.

When you refactor, you are improving the design of code after it has been written.
Ideally, in a perfectly written computer program, with a perfectly designed computer language, with a perfect IDE, it should be possible to change any line / element of the program in isolation without having to make any supporting changes elsewhere.
The program should still function in a reasonable way. For example, you should be able to change the allowable bounds on a field variable in one line of code (which might result in it internally being promoted from int to long), without having to manually adjust any other code.
The basic principle is you should have to tell a computer any one fact, or decision in only one place.
For example, you should have to tell a program which colour scheme to use inprecisely one and only one place, and by "one place" I mean one line of code.

Some of the most useful refactorings are : 

Extract Method
I highlight a block of code that might be many line or just an expression. It then turns it into a method, figures out what the parameters need to be, and what type it returns. It then replaces the code with a call to the new method. It then finds similar code snippets and, with permission, also converts them to calls the new method.

Introduce constant
Converts literals to named constants. You have to give it some help to decide which instances to convert.

Change signature
Add, remove, rename, reorder the parameters of a method and automatically modify all invocations to the new rules.

Global rename
Most common refactoring. It drives the decision of what code belongs where. All IDEs renames with knowledge of Java syntax, so you don’t accidentally rename unrelated local variables with the same name. Lets you review which comments to rename. Lets you also rename similarly named variables and method in the same pop.

Pull up
Move a method or field from subclass to its superclass and make necessary adjustments.
It will let you control just how much of its attached baggage of fields and methods you move with it.

Push down
Move a method or field from a superclass to one of its subclasses.

Generify
Add generics to a class.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.