Saturday, November 19, 2016

Refactoring to Patterns: About Refactoring

I am reading this pretty old (2004) book by Joshua Kirievsky called Refactoring to Patterns, which I would like to study deeper and share what I learn with others.

The book, as suggested by its title, combines two key concepts in software development: Refactoring and Design Patterns. In fact, the book argues that great software designs are better understood and learnt not as stand-alone masterpieces of software craftsmanship, but by studying how they emerge through refactoring.

What is Refactoring?

Refactoring is behavior-preserving transformation, "a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior." [Martin Fowler]

Refactoring involves
  • removing duplication
  • simplifying complex logic
  • and clarifying unclear code.
To refactor safely and with courage, you need a set of automated tests that you can run quickly to confirm that your code still works.

Refactoring should be done in small steps that take seconds or minutes.

It's better to refactor continuously, rather than in phases. If you see code that needs improvement, improve it. If you need to implement an important feature by tomorrow, first finish the feature and refactor later. Refactoring must co-exist harmoniously with business priorities.

Refactoring Motivations

  • Make it easier to add new code: when adding a new feature, we can use two approaches, none of which is right or wrong:
    • either code it quickly without regard to how well it fits with an existing design, refactor later;
    • or modify the existing design so it can easily and gracefully accommodate the new feature.
  • Improve the design of existing code: sniff constantly for code smells and remove smells immediately (or soon) after finding them. This is a great hygienic habit. It can also lead to greater job enjoyment.
  • Gain a better understanding of code: If some code is not clear, it's an odor that needs to be removed by refactoring, not by deodorizing the code with a comment.
  • Make coding less annoying: We often refactor simply to make code less annoying to work with.

Many Eyes

To get best refactoring results, you'll want the help of many eyes, which is one of the reasons for the practices of pair-programming and collective code ownership.

Human-Readable Code

Good code 
  • reads like spoken language
  • separates important code from distracting code
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." [Martin Fowler]

Keeping Code Clean

Refactoring is a lot like cleaning your room. The worse the mess becomes, the harder it is to clean and the less you want to clean it. One giant cleanup is not enough, you must practice continuous hygiene. To keep code clean, we must continuously:
  • remove duplication
  • simplify code
  • clarify code
Do not tolerate messes in code, and do not backslide into bad habits.

Clean code -> Better design -> Faster development -> Happy customers and programmers 

Small Steps

Take very small steps and keep the unit tests green! They should not stay red for more than a few minutes.

Design Debt

Design debt is a much better metaphor to communicate with management than the technical language of refactoring. It occurs when you do not consistently do 3 things:
  1. Remove duplication
  2. Simplify your code
  3. Clarify your code's intent
When do you pay back the debt? In financial terms, when you don't pay your debt, you incur late fees. If you don't pay your late fees, you incur higher late fees, and so on. Compound interest kicks in, and going out of debt becomes an impossible dream. So it is with design debt.

Evolving a New Architecture 

Evolutionary design suggest that you:
  • Form one team
  • Drive the framework from application needs
  • Continuously improve applications and the framework by refactoring

Composite and Test-Driven Refactorings

Composite refactorings are high-level refactorings composed of low-level refactorings. Between applying low-level refactorings you run unit tests.

Test-driven refactorings involove applying TDD to produce replacement code and then swap out old code for new code (while retaining and rerunning the old code's tests).
When it's impossible to evolve a design through composite refactorings, test-driven refactorings can be used to produce a better design. Substitute Algorithm is a good example of a test-driven refactoring.

Benefits of composite refactorings:
  • They describe an overall path for a refactoring sequence
  • They suggest non-obvious design directions
  • They provide insights into implementing patterns

15 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in
    python training Course in chennai | python training in Bangalore | Python training institute in kalyan nagar

    ReplyDelete
  3. I have picked cheery a lot of useful clothes outdated of this amazing blog. I’d love to return greater than and over again. Thanks! 
    Java training in Indira nagar | Java training in Rajaji nagar

    Java training in Marathahalli | Java training in Btm layout

    ReplyDelete
  4. I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  5. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  6. Hi,
    Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take big data hadoop training in pune. Because big data course in pune is one of the best that one can do while choosing the course.

    ReplyDelete
  7. This post is very helpful and informative. Keep Posting such an informative post in the future.

    Accident Attorney Fort Lauderdale

    ReplyDelete
  8. Superb and helpful blog..
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    If you need any logo then,
    Please visit our site,
    buylogo

    ReplyDelete
  9. Need The To Hire A Hacker❓ Then contact PYTHONAX✅

    The really amazing deal about contacting PYTHONAX is that the Hack done by us can’t get traced to you, as every Hacking job we do is strongly protected by our Firewall. It’s like saying if anyone tries to trace the Hack, it will lead them to us and we block whatever actions they are doing.

    We have been Invisible to Authorities for almost a decade now and if you google PYTHONAX, not really about us comes out, you can only see comments made by us or about us.

    Another Amazing thing to you benefit from Hiring our Hackers is that you get a Legit and the best Hacking service, As we provide you with Professional Hackers who have their Hacking Areas of specialization.
    We perform every Hack there is, using special Hacking tools we get from the dark web.

    Some list of Hacking Services we provide are-:
    ▪️Phone Hacking & Cloning ✅
    ▪️Computer Hacking ✅
    ▪️Emails & Social Media Account Hacking✅
    ▪️Recovering Deleted Files✅
    ▪️Tracking & Finding People ✅
    ▪️Hunting Down Scammers✅
    ▪️Hack detecting ✅
    ▪️Stealing/Copying Files & Documents From Restricted Networks and Servers ✅

    OTHER SPECIAL HACKING SERVICES

    ▪️Binary Option Recovery ✅
    ▪️Scam Money Recovery✅
    ▪️Bitcoin Multiplication✅
    ▪️Change Of Grades In Universities/Colleges ✅
    ▪️Phone Calls Monitoring✅
    ▪️keyLogging Installation✅
    ▪️Remote Access Trojan (RAT) installation ✅
    ▪️Cyber Security Upgrade✅
    ▪️And lots more...........

    Whatever Hacking service you require, just give us an Email using the Emails Address provided below.
    pythonaxhacks@gmail.com
    pythonaxservices@gmail.com

    PYTHONAX.
    2020 © All Right Reserved.

    ReplyDelete
  10. Wow, amazing weblog format! How lengthy have you been running a blog for? you make running a blog look easy. The total glance of your website is wonderful, let alone the content!
    Training in Velachery | Certification | Class Room | Online Training

    SET-6 ORACLE



    oracle training in chennai

    oracle training in velachery

    oracle dba training in chennai

    oracle dba training in velachery

    ccna training in chennai

    ccna training in velachery

    seo training in chennai

    seo training in velachery

    ReplyDelete
  11. Very nice this blog . this blog is very informative.such a great blogging website.

    "woman wine expert in usa
    "

    ReplyDelete