开发者

Does re-factoring of code affect the performance of an application?

开发者 https://www.devze.com 2023-03-21 19:56 出处:网络
I have some decent knowledge of programming in jav开发者_开发技巧a. Although I am not aware of the industrial standards of coding. I see that, people insist on adhering to design patterns. They consta

I have some decent knowledge of programming in jav开发者_开发技巧a. Although I am not aware of the industrial standards of coding. I see that, people insist on adhering to design patterns. They constantly emphasize on refactoring code. Although I can understand the positives of refactoring, I feel that refactoring may sometimes effect the performance.

For ex: Consider this case where I dont refactor code.

Class Aclass{

    function one(){
    ........
    two();
    }

    function two(){
    ........
    }

}

Class MainClass{
    main(){
    Aclass obj = new Aclass();
    obj.one();
    }
}

Considering that this is the initial code snippet that I implement. In this case I have one object of Aclass in the memory and the function one calls the function two. All this happens within the scope of one instance of Aclass.

Suppose If I refactor the code and shift function two to an another class Bclass. The code might look like:

Class Aclass{

        function one(){
        ........
        Blass bclass = new Bclass();
        bclass.two();
        }
}

Class Bclass{
        function two(){
        .....
        }
}


Class MainClass{
    main(){
    Aclass obj = new Aclass();
    obj.one();
    }
}

When I refactor the code as above, I might be under the need to create a new Object for Bclass. IF this kind of refactoring is done on a large scale for a huge application, more number of instances would get accumulated in the memory and that would certainly effect the performance of an application, Right?

So as per the example shown above, Does it mean that re factoring (might) effect the performance of an application? Or if I am wrong in my assumption, Please let me know how to deal with this issue in programming?

Thanks


A refactoring could indeed affect the performance, but not always in the ways that you expect. Martin Fowler gives a nice example in his book "Refactoring: improving the design of existing code". In this example, separating one loop into two actually improved the performance. Before the refactoring, the processor had to switch between two areas of memory - after the refactoring, it could exclusively look at one area and then exclusively at the other, (probably) allowing for compiler optimizations.

When performance is a concern, my advice would be to use the profiler to identify where the problem issues are. Then you know where you should optimize your code.

At any rate be careful of premature optimization.

I hope this is useful to you.


One of the best ways refactoring can affect performance is by isolating the slow sections of your code, so you can focus your optimization attention there. It's very hard to optimize a long method. Easier to optimize a tiny method, and easier to see when there's no more optimization to make.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号