Refactoring Good Code
Published: 2022-12-29 18:29:56

You might be wondering what a 2nd year ICT student has to say to already existing good code. Well, you are about to find out as today we are going to look up a random public repo on GitHub and are going to apply some good old refactoring to them. To start off, it took me ages to find a repo to begin with yet I stumbled upon one eventually which would be this one: [Repo](https://github.com/wickedwukong/martin-fowler-refactoring-2nd). Now you might ask yourself what can be improved upon code, which is supposed to teach you refactoring, well the more you mess around the more you find out. Let’s dig into it.

I put a little time into finding a couple files which may not be that clear to my brain and eventually I found one. So, if you so happen to open the repo go into the ‘src’ folder then the ‘chp8’ folder you will find a small innocent file named ‘MoveFunction1.js’. Inside of that file is a function called ‘distance’ which takes two parameters and calculates something. Now distance is not very much descriptive now is it, so I saw the comment inside of the function as I did not understand the math behind the whole function to calculate the distance between two points. It happens to be a formula I had never heard of as my math understanding is still at high school graduation level, yet I figured that the whole function could be refactored in a way for people like me to understand it better, so I got working. I turned every math equation done within the ‘distance’ function into its own separate function with proper naming to first have a better overview of what the code does and second to understand the math behind every step, then I renamed the variable names as they were not very much descriptive. After doing that refactoring I got rid of the comment as now the code is clear enough to be understood and if someone does not get it, they can simply compare the code to a simple google search. The code smells which I found according to Refactoring Guru were ['Comments'](https://refactoring.guru/smells/comments ), due to the need of doing separate research using the link in the comment, and [‘Long Method’] (https://refactoring.guru/smells/long-method ), as the functions is rather long due to the complex math. The method used to refactor according to Refactoring Guru is the [‘Extract Method’] (https://refactoring.guru/extract-method ), as we are extracting code and putting it into a separate function and make use of the independent function. 

One rising question may be that if the refactoring is even necessary and to be honest, I am unsure. First off, the code is very clear already and the comment provides documentation for the user to be able to understand the code after doing some research and digging into the maths behind it. On the other hand, I must say that it is very much needed than I as a man of simple matters do not want to understand the math behind formulas I will never look up again. I create an independent function once and reuse it as often as possible as I do my research on the matter once and then never again as at some point, I did understand the logic and it worked consistently. So, what more do I need than a reliable function I can keep copy pasting without thinking much of it, exactly nothing then we are lazy people trying to find an easy way around. As a follow up you will find two code sources, one of the already existing code and the second being my refactored code. 

```javascript
function distance(p1, p2) {
    // haversine formula see http://www.movable-type.co.uk/scripts/latlong.html
    const EARTH_RADIUS = 3959; // in miles
    const dLat = radians(p2.lat) - radians(p1.lat);
    const dLon = radians(p2.lon) - radians(p1.lon);
    const a = Math.pow(Math.sin(dLat / 2), 2)
        + Math.cos(radians(p2.lat))
        * Math.cos(radians(p1.lat))
        * Math.pow(Math.sin(dLon / 2), 2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return EARTH_RADIUS * c;
}

function radians(degrees) {
    return degrees * Math.PI / 180;
}
```

```javascript
function distance(p1, p2) {
    // haversine formula see http://www.movable-type.co.uk/scripts/latlong.html
    const EARTH_RADIUS = 3959; // in miles
    const dLat = calculateRadiansBetweenTwoPoints(p1.lat, p2.lat);
    const dLon = calculateRadiansBetweenTwoPoints(p1.lon, p2.lon);
    const UNKNOWN_IN_HAVERSINCE = calculateUnknownInHaversineInFormula(dLat, dLon, p2.lat, p1.lat)
    const ANGULAR_DISTANCE_RADIANS = calculateAngularDistance(UNKNOWN_IN_HAVERSINCE) 
    return EARTH_RADIUS_MILES * ANGULAR_DISTANCE_RADIANS;
}

function calculateDeltaRadiansBetweenTwoPoints(p1, p2) {
    return radians(p2) - radians(p1)
}

function calculateAngularDistance(UNKNOW_IN_HAVERSINE) {
    return 2 * Math.atan2(Math.sqrt(UNKNOWN_IN_HAVERSINE), Math.sqrt(1 - UNKNOWN_IN_HAVERSINCE));
}

function calculateUknownInHaversinceFormula(dLat, dLon, p2Lat, p1Lat) {
    return Math.pow(Math.sin(dLat / 2), 2)
       + Math.cos(radians(p2Lat))
       * Math.cos(radians(p1Lat))
       * Math.pow(Math.sin(dLon / 2), 2);
}

function radians(degrees) {
    return degrees * Math.PI / 180;
}
```