Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mariusz Sieraczkiewicz - Natural course of refactoring


Published on

Doing refactoring seems to be obvious practice in a software development. In many teams times it is not being done at all. "We have no time to do it!". "We are not allowed". "We don't know where to start and how to do it pragmatically". Many programmers are familiar with classic refactorings or even more complex refactorings to patterns, clean code rules and naming rules. But it is not enough. Pragmatic refactoring is a difficult practice that requires many years of experience. And what if you or folks from your team don't have that experience? Then refactoring may be more harmful than useful. The Natural Course of Refactoring is a process, workflow that expresses something what may seem obvious for experienced refactoring practitioner but very difficult to figure out for beginners or even more advanced practitioners. It is like red-green-refactor for TDD. NCR is a way of thinking which shifts your focus from single refactorings to a more holistic approach. It is also decision framework helping you to specify what is next pragmatic step (if there is any).It helps you to recognize what stage the particular fragment of source code is, what you can do about it and decide if to do something or leave it as it is. It guides you what exact refactorings you should take into consideration in the particular stage and also which refactorings are way too big steps. Who can benefit from this?

Every programmer beginning refactoring practice or having need for clear guidance about refactoring process.
Every leader who wants refactoring to become an everyday practice even in inexperience team.
Every coach who wants to have a simple yet powerful framework to teach refactoring as a whole process (not only weakly connected pieces).

Published in: Software
  • Be the first to comment

  • Be the first to like this

Mariusz Sieraczkiewicz - Natural course of refactoring

  1. 1. Natural Course of Refactoring. A refactoring workflow. Mariusz Sieraczkiewicz @ms_bnsit_pl 1
  2. 2. Why refactoring is like sex? NaturalCourse of Refactoring
  3. 3. Once you get started, you’ll only stop because you’re exhausted. One mistake and the consequences may be really huge. Whyrefactoring is like sex? NaturalCourseofRefactoring2014 3 (adaptedfrom
  4. 4. People more talk about it than actually do. You can do it for money or for fun. It’s not really an appropriate topic for dinner conversation. Whyrefactoring is like sex? NaturalCourseofRefactoring2014 4 (adaptedfrom
  5. 5. Beginners do a lot of noise about it. Some people are just naturally good at it, .. but some people will never realize how bad they are, and you’re wasting your time trying to tell them. Whyrefactoring is like sex? NaturalCourseofRefactoring2014 5 (adaptedfrom
  6. 6. It doesn’t go so well when you’re drunk, but you’re more likely to do it. You’ll miss it after a longer break. Whyrefactoring is like sex? NaturalCourseofRefactoring2014 6 (adaptedfrom
  7. 7. Code readability NaturalCourse of Refactoring
  8. 8. .so!changesIntroDuCinG!maYBe,softwaRe,TosO,cal LEdprOgRessIve,and.however.cHanGes! uallY,sTRucTureOFmaYbe,hoWEVeRtHecoDE,And!w HaTmaYbecumulatEdhowevEr.anD,mAkes.AnD,,and ,the.LeSs!rEAdAblE,aNd.cOdeMAybe.ANd,!and,!Th enumber!of,sO,HoWEvErsodepeNdeNCiES,And.MA ybeintErACtIOns!betWeenHoWevEr!differEntsySte m.moDules!inCreasESoThat,iTsO!,T o,AnDuNdErstandmodIFy Why is it difficulttounderstand software aftersome timeof its evolution? NaturalCourseofRefactoring2014 8
  9. 9. Introducing changes to software (so called progressive changes) usually modifies structure of the code, what cumulated makes the code less readable. The number of dependencies and interactions between different system modules increase, so that it is more difficult to understand and modify. Why is it difficulttounderstand software aftersome timeof its evolution? NaturalCourseofRefactoring2014 9
  10. 10. Even answers to easy questions are difficult to find. The problem lies more in a code readibility, than in the programmers reading skills. Even great reader will not perform good in such environment. What alters programmersproductivity in this case? NaturalCourseofRefactoring2014 10
  11. 11. Flavoursof refactoring NaturalCourse of Refactoring NaturalCourseofRefactoring2014 11
  12. 12. Twoflavours of refactoring NaturalCourseofRefactoring2014 12
  13. 13. Everyday refactoring Within a reach of every programmer Can be done in minutes Mostly safe, IDE-base automatic refactorings For local health of the code Part of programming practice No excuse for not doing it Main practices Cleaning names Naming conditions Extracting methods Moving methods and extracting classes Everyday refactoring NaturalCourseofRefactoring2014 13
  14. 14. Strategic refactoring A team longer term effort Requires aligning with iteration planning Generates items in backlog Risky activity that requires intensive testing (including good tests suite) Difficult and time-consuming Check carefully if refactoring gives you enough value (Feather’s Quadrant) Main practices Introducing high level design patterns Introducing architectural changes Strategic refactoring NaturalCourseofRefactoring2014 14
  15. 15. When should I do strategic refactoring? NaturalCourse of Refactoring
  16. 16. 1. complexity := 1 2. For every decision point (if, while, for, and, or, try, case, break, continue) complexity++ 3. if 6 <= complexity <= 10 – simplify a method 4. if complexity > 10 – compose method McCabe Metric (Cyclomatic Complexity) Simplifiedversion NaturalCourseofRefactoring2014 16
  17. 17. High complexity/ Seldom changes Don’t touch it. High complexity/ Frequent changes Apply strategic refactoring Low complexity/ Seldom changes Utils, good for experiments Low complexity/Frequent changes Heaven  Feather’sQuadrant NaturalCourseofRefactoring2014 17 Confront with: • team opinions • business strategy Frequency of changes Complexity
  18. 18. Natural Course of Refactoring Theprocess
  19. 19. NaturalCourseofRefactoring2014 19
  20. 20. Step 0. Understandthecode NaturalCourseofRefactoring2014 20
  21. 21. Sometimes it is the most difficult point Try to… • find an author and ask for help • find somebody who worked with the code and ask for help • find somebody who knows the system (or module) and ask for help • Do it yourself if none of above are possible Understand NaturalCourseofRefactoring2014 21
  22. 22. Mental tools Simple code cleaning Clean up the names Add temporary comments to the code Introduce lazy variables initialization Make optical cleanup (make more space) Scaffolding refactoring Do some exploratory refactoring to be thrown away The only goal is to gain more understanding of the code Done in Notepad or something similar Understand NaturalCourseofRefactoring2014 22
  23. 23. They should be temporary. Delete them after refactoring. // SMELL it doesn’t look good, copy- paste antipattern // REFACTOR introduce factory // NOTE send a message // IDEA it can be modeled as a state machine Introducerefactoringcomments NaturalCourseofRefactoring2014 23
  24. 24. Step 1. Express algorithm NaturalCourseofRefactoring2014 24
  25. 25. Aim Code that speaks to you Mental tools • Compose method • Introduce Method Object Refactoring • Extract method Express algorithm NaturalCourseofRefactoring2014 25
  26. 26. Step 2. Extractresponsibilities NaturalCourseofRefactoring2014 26
  27. 27. Move meaningful private methods to another classes. You can’t test private method without hacks so if it is worth testing, find another class where this method will be public. Extract responsibilities NaturalCourseofRefactoring2014 27
  28. 28. Mental tools Single responsibility principle Move method refactoring Extract class refactoring Introduce Domain Object Introduce Value Object Extract responsibilities NaturalCourseofRefactoring2014 28
  29. 29. Step 3. Introduceflexibility NaturalCourseofRefactoring2014 29
  30. 30. Flexibility based on requirements eg. be prepared for many cripting algorithms, starting with 5 Flexibility which emerged from code eg. your team has written 8 reports with similar structure Never guess for flexibility Do you REALLY needflexibility? NaturalCourseofRefactoring2014 30
  31. 31. Strategy, Template method – different versions of the same algorithm State – simple state machines Factory – polimorphic object creation Builder – creating complex object in multiple steps Decorator – flexibility in enhancing behaviour at runtime Observer – loosen dependencies by indirect communication (events) Some design patterns applications NaturalCourseofRefactoring2014 31
  32. 32. Mental tools S.O.L.I.D. Design patterns Refactoring to patterns Apply designpatterns NaturalCourseofRefactoring2014 32
  33. 33. Step 4. Evolve architecture NaturalCourseofRefactoring2014 33
  34. 34. It is always a strategic decision. It is useful to have regular Design Retrospective meetings when such ideas are discussed. Evolve architecture NaturalCourseofRefactoring2014 34
  35. 35. Mental tools Introducing/removing layers Introducing or replacing ORM/NoSQL/? Important change in building blocks Changing or introducing new framework Introducing events Introducing state machine Moving towords DDD, Microservices, CQRS Introducing Bounded-Context (DDD) Applying Anticorruption Layer Evolve architecture NaturalCourseofRefactoring2014 35
  36. 36. NaturalCourseofRefactoring2014 36
  37. 37. NaturalCourseofRefactoring2014 37 Everyday refactoring Strategic refactoring
  38. 38. It is just a model – neither true nor false Big classes and big methods has fragments of code in different NCR stages Quite seldom code is in Compose method step (which the most increases readability) The consecutive steps are done more seldom than preceding ones Some comments NaturalCourseofRefactoring2014 38
  39. 39. Extracting responsibilities and Introducing flexibility should be attracted by current architecture vision (Architectural Mantra) Lack of the architecture vision may push you to local optimum NCR is an ongoing process and should be a part of whole software process. NCRand architecture NaturalCourseofRefactoring2014 39 Technikipracyz kodem
  40. 40. Why NCR? Easy to teach Easy to understand and remeber Separates everyday and strategic refactoring Indicates the simplest (safe) possible step in the moment Gives hints what kind of refactorings can be applied in the moment NCRIN A TEAM NaturalCourseofRefactoring2014 40
  41. 41. Natural Course of Refactoring. A refactoring workflow. Mariusz Sieraczkiewicz @ms_bnsit_pl 41