Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted February 12th, 2016
What is code refactoring, and why do we do it?

Code refactoring is the process of restructuring and improving existing code. This means that we change the code without changing it’s behaviour yet improves its internal structure. A few of the advantages are improved readability, extensibility* and reduced complexity, while we increase the maintainability* of the code. Refactoring is typically done in small steps, which, after each step, means the application is still working and functionally unchanged. The reason we refactor in small steps is because some applications are written in so called “spaghetti code”*, thus making small steps easier to maintain.

Refactoring is not rewriting, although many people just assume they are the same. And they have many good reasons to back that up. For example, they have to refactor an implemented hack. But the technical difference between the two is that refactoring, as stated above, doesn’t change the functionality whereas rewriting does (rewriting is reworking).

How do you refactor?

Before you begin refactoring you make sure the part you’re going to refactor works in the first place. Most companies use unit tests, which is a software development process in which the smallest testable parts of an application, called units, are individually tested.

After you’ve properly tested everything you can refactor small pieces, but keep in mind you maintain the D.R.Y.* method. Don’t be afraid to do baby steps. Do not do two things at the same time. If you remark something else requiring refactoring, note it down, do not fix it right now, even if it seem very easy, as this could distract you from your current progress.But keep in mind to re-run the tests frequently so if something stops working you can fix it instantly.

Example

Here’s an example from the current CodePamoja project, where we need to log an user out. Currently we use a “hack” to force reload the window, remove the authentication service and clear the rootscope*, while this could be done with one function.

scope.userLogout = function () {
    $auth.logout()
        .then(function () {
            authenticationService.remove();
        })
        .then(function () {
            $window.location.reload();
        })
        .then(function () {
            $rootScope = undefined;
            $location.path(ROUTE.LOGIN);
        })
};

Becomes:

scope.userLogout = function () {
    $auth.logout()
};

Conclusion

Refactoring is basically perfecting your code and/or structure, or in other words, cleaning up after yourself. It should be an important task for every developer, no matter how small or big the project is that you’re working on.

Explanation:

Extensibility: It is easier to extend the functionality of an application.
Maintainability: It is easier to fix bugs and implement new features because of the improved readability and/or better structured coding.
Spaghetti code: Spaghetti code is a phrase for code that has a complex and tangled control structure. It is named such because the program flow is conceptually like a bowl of spaghetti, or in other words; twisted and tangled.
D.R.Y.: Don’t Repeat Yourself.
rootscope: Every application has a single root scope. All other scopes are descendant scopes of the root scope. Scopes provide separation between the model and the view, via a mechanism for watching the model for changes. They also provide event emission/broadcast and subscription facility. (Source)

Posted by Michael Vrolijk

Leave a Reply

Your email address will not be published. Required fields are marked *