Refactoring - Falmouth-Games-Academy/comp350-research-journal GitHub Wiki

Refactoring

What is Refactoring?

"Refactoring is the process of changing a software system in such a way that it does not alter the external behaviour of the code yet improves its internal structure." 1(https://www.amazon.com/Refactoring-Improving-Design-Existing-Code-dp-0201485672/dp/0201485672/ref=mt_hardcover?_encoding=UTF8&me=&qid=) The definition provided by E. Murphy-Hill et all 2(https://ieeexplore.ieee.org/document/6112738) does not include the requirement that for something to be considered refactoring the codes internal structure must be improved. In most cases, it could be assumed refactoring may be done to improve the internal structure of the program. As the term improve is subjective individuals may find it hard to agree on what constitutes a code refactor and what does not.

Refactoring code is an almost essential part of software development, this can be due to a number of reasons. Usually, code is refactored to improve its readability, but it can also be refactored to improve performance. Code can also have to be refactored to save money and time in the future making it more maintainable. Repeating the same code is not good for maintainability so refactoring it can save you from this mistake lots of people make. It is a good standard in programming to make sure other developers would be able to jump into your project and be able to get straight to work so ensuring it is refactored to the highest standard is good 3(https://www.telerik.com/blogs/top-5-reasons-why-you-should-refactor-your-code).

Examples of Refactoring

A rather simple example of refactoring code could be changing multiple if and else if statements into a switch case, or make a dictionary which could be

// This Function converts an integer between zero and two into a string representing the number.
string exampleFunction(int example)
{
    if(example == 0)
    {
        return "Zero";
    }

    else if(example == 1)
    {
        return "One";
    }

    else if(example == 2)
    {
        return "Two";
    }

    else
    {
        return "Not a number between 0 and 2";
    }

} 

Switch Case

// This Function converts an integer between zero and two into a string representing the number.
string exampleFunction(int example)
{
    String result = "";
    switch (example) 
    {
    case "0":
        result = "Zero";
        break;
    case "1":
        result = "One";
        break;
    case "2":
        result = "Two";
        break;
    Default:    
        result = "Not a number between 0 and 2";
        break;

    return result;
} 

Dictionary

// Initialise the Dictionary at the start for use later.
Dictionary<int, string> exampleDict = new Dictionary<int, string>()
                                                            {
                                                                {0,"Zero"},
                                                                {1,"One"},
                                                                {2,"Two"}
                                                            };
//Function used to access the Dictionary
string exampleFunction(int example)
{
    string result = "";
    if(exampleDict.TryGetValue(example, out result))
    {
        return result;
    }

    else
    {
        return "Not a valid number between 0 and 2";
    }
}

Switch vs if/else performance

Benchmark tests between switch and if statements can be found to be 25% faster 4(http://www.blackwasp.co.uk/speedtestifelseswitch.aspx). Typically, a compiler deals with switch statements by building a jump table in the assembly code and using that to determine the appropriate route instead of using comparators like if/else 5(https://stackoverflow.com/questions/36650039/switch-vs-if-else-performance). However, for small cases, this may be less efficient than the compare and jump branch table approach of if/else interpreted by a compiler.

Regardless, unless dealing with extraordinarily large numbers of input variables, this performance increase is so small that maintainability and readability should take precedence 6(https://www.geeksforgeeks.org/switch-vs-else/) 7(https://stackoverflow.com/questions/2086529/what-is-the-relative-performance-difference-of-if-else-versus-switch-statement-i). While academically interesting, such micro optimisation is what Knuth refers to when stating premature optimisation is the root of all evil (see Profiling).

Should I refactor?

Refactoring code can take a lot of time for much larger scale programs, this can also be very costly as it can takes developer time to make the changes and the new version of the code may not necessarily be an improvement. It can also lead to system downtime in some cases which can also be quite costly. Research carried out by R. Leitch et al. discusses ways to understand the economic value of refactoring and give a better idea of how much value will actually be added through refactoring 8(https://pdfs.semanticscholar.org/5e3f/7ddfacfe4aec50094334f457ae255c6a2260.pdf).

Although Refactoring can be a costly and time consuming process, it is a very important step in the development cycle as it can improve the code base to no end, making it much more maintainable9(https://en.wikipedia.org/wiki/Code_refactoring#Benefits), and things such as Code Smells can make the process of deciding when to refactor much easier(see Code Smell)

References

[1] Fowler, M., 2018. Refactoring: improving the design of existing code., Addison-Wesley Professional.

[2] E. Murphy-Hill, C. Parnin, and A. P. Black, โ€œHow we refactor, and how we know itโ€(https://ieeexplore.ieee.org/document/6112738), IEEE Transactions on Software Engineering, vol. 38, no. 1, pp. 5โ€“18, 2012.

[3] https://www.telerik.com/blogs/top-5-reasons-why-you-should-refactor-your-code

[4] BlackWasp - Speed Test: Switch vs If-Else-If, accessed on 10th Febuary 2019.

[5] Stack Overflow - Switch vs If-Else Performance, accessed on 10th Febuary 2019.

[6] GeeksforGeeks - switch vs if else, accessed on 10th Febuary 2019.

[7] Stack Overflow - What is the relative performance difference of if/else versus switch statement in Java?, accessed on 10th Febuary 2019.

[8] Leitch, R. and Stroulia, E., 2003, May. Understanding the economics of refactoring. In EDSER-5 5th International Workshop on Economic-Driven Software Engineering Research (p. 44).

[9] https://en.wikipedia.org/wiki/Code_refactoring#Benefits