<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thimayanelomi on Medium]]></title>
        <description><![CDATA[Stories by Thimayanelomi on Medium]]></description>
        <link>https://medium.com/@thimayanelomi2001?source=rss-4701d51724d9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*u6YPi7d9-Fh6KwHB</url>
            <title>Stories by Thimayanelomi on Medium</title>
            <link>https://medium.com/@thimayanelomi2001?source=rss-4701d51724d9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 15:43:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thimayanelomi2001/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Software Refactoring: Improving Code Quality Without Changing Functionality]]></title>
            <link>https://medium.com/@thimayanelomi2001/software-refactoring-improving-code-quality-without-changing-functionality-30ee32e52e0d?source=rss-4701d51724d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/30ee32e52e0d</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[code-refactoring]]></category>
            <dc:creator><![CDATA[Thimayanelomi]]></dc:creator>
            <pubDate>Fri, 13 Nov 2026 17:56:31 GMT</pubDate>
            <atom:updated>2026-11-13T17:56:31.635Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vH-aGcyHXq4PebwMFD8W8A.png" /></figure><h3>1. Introduction</h3><p>When developing software, programmers often focus on making their code work correctly. However, working code is not always clean, readable, or easy to maintain. As software projects grow, the code can become complicated and difficult to understand.</p><p>This is where <strong>software refactoring</strong> becomes important.</p><h3>What is Software Refactoring?</h3><p>Software refactoring is the process of improving the internal structure of existing code without changing how the software behaves externally.</p><p>In simple words, refactoring means making code cleaner and easier to understand while keeping the same functionality.</p><p>For example, imagine a program that calculates the total price of products. The program works correctly, but its code contains unnecessary variables and repeated calculations. We can refactor the program to make it simpler without changing the final result.</p><h3>Why is Refactoring Important?</h3><p>Refactoring provides several benefits:</p><ul><li><strong>Improves readability:</strong> Clean code is easier for developers to understand.</li><li><strong>Reduces complexity:</strong> Simple code is easier to manage.</li><li><strong>Improves maintainability:</strong> Developers can update the software more easily.</li><li><strong>Reduces duplication:</strong> Repeated code can be replaced with reusable functions.</li><li><strong>Supports debugging:</strong> Well-organized code makes it easier to identify problems.</li><li><strong>Supports future development:</strong> New features can be added with less difficulty.</li></ul><p>Refactoring is an important practice in software development because it helps developers maintain code quality throughout the software development life cycle.</p><h3>2. Understanding Code Smells</h3><p>Before refactoring code, developers need to identify areas that require improvement.</p><p>A <strong>code smell</strong> is a sign that the code may have a design or maintainability problem.</p><p>Code smells do not always mean the program contains errors. However, they can make the code difficult to understand, test, and maintain.</p><p>Let’s discuss two common code smells.</p><h3>2.1 Duplicate Code</h3><p>Duplicate code occurs when the same or similar code appears in multiple places in a program.</p><p>For example:</p><pre># Calculate the total for customer 1<br>price1 = 1000<br>quantity1 = 2<br>total1 = price1 * quantity1<br>print(&quot;Customer 1 Total:&quot;, total1)</pre><pre># Calculate the total for customer 2<br>price2 = 1500<br>quantity2 = 3<br>total2 = price2 * quantity2<br>print(&quot;Customer 2 Total:&quot;, total2)</pre><p><strong>Problem:</strong></p><p>The same calculation is repeated for different customers.</p><p>If the calculation logic changes, developers must update every repeated section.</p><p><strong>Solution:</strong></p><p>Create a reusable function that performs the calculation.</p><p>This technique is called <strong>Extract Method</strong>, which we will discuss later.</p><h3>2.2 Long Method</h3><p>A long method occurs when a single function performs too many tasks.</p><p>For example:</p><pre>def process_order(price, quantity):<br>    total = price * quantity</pre><pre>    discount = total * 0.10<br>    final_total = total - discount</pre><pre>    tax = final_total * 0.05<br>    amount = final_total + tax</pre><pre>    print(&quot;Total:&quot;, total)<br>    print(&quot;Discount:&quot;, discount)<br>    print(&quot;Tax:&quot;, tax)<br>    print(&quot;Final Amount:&quot;, amount)</pre><p><strong>Problem:</strong></p><p>This function performs several responsibilities:</p><ul><li>Calculates the total price.</li><li>Calculates the discount.</li><li>Calculates the tax.</li><li>Displays the final bill.</li></ul><p>When a function handles many responsibilities, it becomes more difficult to understand and modify.</p><p><strong>Solution:</strong></p><p>Separate the responsibilities into smaller functions.</p><p>This improves readability and makes individual calculations easier to test.</p><h3>3. Three Common Refactoring Techniques</h3><p>Developers use different refactoring techniques to improve code quality.</p><p>In this section, we will explore three techniques with practical Python examples.</p><h3>Technique 1: Extract Method</h3><h3>What is Extract Method?</h3><p>Extract Method is a refactoring technique that moves a section of code into a separate function.</p><p>It is useful when the same logic is repeated or when a function performs too many tasks.</p><h3>Before Refactoring</h3><p>Consider a program that calculates the total price for two customers.</p><pre>price1 = 1000<br>quantity1 = 2</pre><pre>total1 = price1 * quantity1<br>print(&quot;Customer 1 Total:&quot;, total1)</pre><pre>price2 = 1500<br>quantity2 = 3</pre><pre>total2 = price2 * quantity2<br>print(&quot;Customer 2 Total:&quot;, total2)</pre><p><strong>Output:</strong></p><pre>Customer 1 Total: 2000<br>Customer 2 Total: 4500</pre><p>The calculation price * quantity is repeated.</p><h3>After Refactoring</h3><p>We can extract the repeated calculation into a separate function.</p><pre>def calculate_total(price, quantity):<br>    return price * quantity</pre><pre>total1 = calculate_total(1000, 2)<br>total2 = calculate_total(1500, 3)</pre><pre>print(&quot;Customer 1 Total:&quot;, total1)<br>print(&quot;Customer 2 Total:&quot;, total2)</pre><p><strong>Output:</strong></p><pre>Customer 1 Total: 2000<br>Customer 2 Total: 4500</pre><h3>How Did Refactoring Improve the Code?</h3><p>The new code is better because:</p><ul><li>It removes repeated calculation logic.</li><li>The function can be reused for other customers.</li><li>Changes to the calculation can be made in one place.</li><li>The code is easier to read and maintain.</li></ul><p>Most importantly, <strong>the output remains exactly the same</strong>.</p><h3>Technique 2: Rename Variable</h3><h3>What is Rename Variable?</h3><p>Rename Variable is a refactoring technique that replaces unclear variable names with meaningful names.</p><p>Good variable names help programmers understand what the code does without requiring additional explanations.</p><h3>Before Refactoring</h3><p>Consider the following code:</p><pre>a = 500<br>b = 3<br>c = a * b</pre><pre>print(&quot;Total Price:&quot;, c)</pre><p><strong>Output:</strong></p><pre>Total Price: 1500</pre><p><strong>Problem:</strong></p><p>The variables a, b, and c do not explain what values they represent.</p><p>Other developers may find it difficult to understand the program.</p><h3>After Refactoring</h3><p>We can replace the unclear variable names with descriptive names.</p><pre>product_price = 500<br>quantity = 3</pre><pre>total_price = product_price * quantity</pre><pre>print(&quot;Total Price:&quot;, total_price)</pre><p><strong>Output:</strong></p><pre>Total Price: 1500</pre><h3>How Did Refactoring Improve the Code?</h3><p>The refactored code is better because:</p><ul><li>Variable names clearly describe their purpose.</li><li>The calculation is easier to understand.</li><li>Other developers can read the code more quickly.</li><li>Future maintenance becomes easier.</li></ul><p>The program still produces the same result.</p><p>This demonstrates that improving code readability does not require changing its functionality.</p><h3>Technique 3: Replace Conditional with Polymorphism</h3><h3>What is Replace Conditional with Polymorphism?</h3><p>This technique replaces repeated conditional statements with separate classes that provide their own implementations of a common method.</p><p>It is especially useful in object-oriented programming when different types of objects perform similar operations in different ways.</p><h3>Before Refactoring</h3><p>Consider a program that calculates employee bonuses based on employee type.</p><pre>def calculate_bonus(employee_type, salary):<br>    if employee_type == &quot;Manager&quot;:<br>        return salary * 0.20<br>    elif employee_type == &quot;Developer&quot;:<br>        return salary * 0.10<br>    else:<br>        return salary * 0.05</pre><pre>print(&quot;Manager Bonus:&quot;,<br>      calculate_bonus(&quot;Manager&quot;, 50000))</pre><pre>print(&quot;Developer Bonus:&quot;,<br>      calculate_bonus(&quot;Developer&quot;, 50000))</pre><p><strong>Output:</strong></p><pre>Manager Bonus: 10000.0<br>Developer Bonus: 5000.0</pre><p><strong>Problem:</strong></p><p>The function contains multiple conditional statements.</p><p>If new employee types are introduced, developers must modify the existing function.</p><p>As the number of employee types increases, the function may become more complicated.</p><h3>After Refactoring</h3><p>We can use separate classes to calculate bonuses.</p><pre>class Employee:<br>    def calculate_bonus(self, salary):<br>        return salary * 0.05<br></pre><pre>class Manager(Employee):<br>    def calculate_bonus(self, salary):<br>        return salary * 0.20<br></pre><pre>class Developer(Employee):<br>    def calculate_bonus(self, salary):<br>        return salary * 0.10<br></pre><pre>manager = Manager()<br>developer = Developer()</pre><pre>print(&quot;Manager Bonus:&quot;,<br>      manager.calculate_bonus(50000))</pre><pre>print(&quot;Developer Bonus:&quot;,<br>      developer.calculate_bonus(50000))</pre><p><strong>Output:</strong></p><pre>Manager Bonus: 10000.0<br>Developer Bonus: 5000.0</pre><h3>How Did Refactoring Improve the Code?</h3><p>The refactored implementation provides several advantages:</p><ul><li>Different employee types have their own bonus calculation methods.</li><li>The code is organized using object-oriented programming principles.</li><li>New employee classes can be introduced without changing the existing bonus methods.</li><li>The code becomes easier to extend and maintain.</li></ul><p>Both implementations calculate the same bonuses for the employee types shown.</p><p>For example, a Manager with a salary of 50,000 receives a bonus of 10,000 in both implementations.</p><p>The same applies to the Developer.</p><p><strong>Important:</strong> This example preserves the calculations for the supported employee types. A complete refactoring should also preserve how the original program handles unknown employee types and invalid inputs.</p><h3>4. Testing After Refactoring</h3><p>Testing is one of the most important steps in software refactoring.</p><p>The main purpose of refactoring is to improve code structure without changing the program’s external behaviour.</p><p>Therefore, developers must verify that the refactored code still works correctly.</p><h3>Why is Testing Important?</h3><p>During refactoring, developers may accidentally introduce errors.</p><p>For example, changing a calculation or removing a condition could produce incorrect results.</p><p>Testing helps developers identify these problems before the software is released.</p><h3>How Can We Test Refactored Code?</h3><p><strong>Step 1: Test the original code</strong></p><p>Before refactoring, run the existing program and record its expected results.</p><p><strong>Step 2: Write automated tests</strong></p><p>Create tests to verify that important functions produce the correct outputs.</p><p><strong>Step 3: Refactor the code</strong></p><p>Improve the internal structure without intentionally changing its behaviour.</p><p><strong>Step 4: Run the tests again</strong></p><p>Execute the same tests after refactoring.</p><p><strong>Step 5: Compare the results</strong></p><p>If the tests pass and the expected behaviour is preserved, the refactoring is more likely to be successful.</p><h3>Practical Testing Example</h3><p>Let’s test the Extract Method example using Python’s unittest module.</p><pre>import unittest</pre><pre>def calculate_total(price, quantity):<br>    return price * quantity<br></pre><pre>class TestCalculateTotal(unittest.TestCase):</pre><pre>    def test_customer_one(self):<br>        self.assertEqual(<br>            calculate_total(1000, 2), 2000<br>        )</pre><pre>    def test_customer_two(self):<br>        self.assertEqual(<br>            calculate_total(1500, 3), 4500<br>        )</pre><pre>    def test_zero_quantity(self):<br>        self.assertEqual(<br>            calculate_total(1000, 0), 0<br>        )<br></pre><pre>if __name__ == &quot;__main__&quot;:<br>    unittest.main()</pre><p>These tests verify three situations:</p><ul><li>A customer purchasing two products.</li><li>A customer purchasing three products.</li><li>A customer purchasing zero products.</li></ul><p>If all three tests pass, the function produces the expected results for these test cases.</p><p>However, passing a few tests does not guarantee that the program behaves correctly for every possible input. Developers should also test boundary conditions, invalid inputs, and other important scenarios.</p><h3>Additional Testing Methods</h3><p>Developers can also use:</p><ul><li><strong>Unit testing:</strong> Tests individual functions or methods.</li><li><strong>Integration testing:</strong> Checks whether different software components work together.</li><li><strong>Regression testing:</strong> Ensures that previously working features continue to function correctly after code changes.</li></ul><p>Using these testing methods reduces the risk of introducing errors during refactoring.</p><h3>5. Conclusion</h3><p>Software refactoring is an essential practice in modern software development.</p><p>It improves the internal structure of existing code while preserving its external behaviour.</p><p>In this article, we discussed two common code smells: Duplicate Code and Long Method.</p><p>We also explored three practical refactoring techniques:</p><ol><li>Extract Method</li><li>Rename Variable</li><li>Replace Conditional with Polymorphism</li></ol><p>Each technique demonstrated how developers can improve code readability, maintainability, and organization without intentionally changing the original functionality.</p><p>We also discussed the importance of testing to verify that refactored code continues to produce the expected results.</p><p>In conclusion, refactoring helps developers transform complicated code into cleaner, more understandable, and more maintainable software.</p><p><strong>Good software is not only software that works correctly. It is also software that developers can easily understand, test, and improve.</strong></p><h3>6. References</h3><ol><li>Fowler, M. (2018). <em>Refactoring: Improving the Design of Existing Code</em> (2nd ed.). Addison-Wesley Professional. <a href="https://martinfowler.com/books/refactoring.html">https://martinfowler.com/books/refactoring.html</a></li><li>Fowler, M. (n.d.). <em>Code Smells</em>. <a href="https://refactoring.com/catalog/">https://refactoring.com/catalog/</a></li><li>Refactoring.Guru. (n.d.). <em>Refactoring Techniques</em>. <a href="https://refactoring.guru/refactoring/techniques">https://refactoring.guru/refactoring/techniques</a></li><li>Python Software Foundation. (n.d.). <em>unittest — Unit Testing Framework</em>. <a href="https://docs.python.org/3/library/unittest.html">https://docs.python.org/3/library/unittest.html</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30ee32e52e0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>