Restructuring Code: From Push To Pull Andreas Leidig, Nicole Rauch - - PowerPoint PPT Presentation

restructuring code from push to pull
SMART_READER_LITE
LIVE PREVIEW

Restructuring Code: From Push To Pull Andreas Leidig, Nicole Rauch - - PowerPoint PPT Presentation

Restructuring Code: From Push To Pull Andreas Leidig, Nicole Rauch June 3, 2013 Our Starting Point Business Software Very poor code quality Planned changes for a module: Bugfixing new features better tests


slide-1
SLIDE 1

Restructuring Code: From “Push” To “Pull”

Andreas Leidig, Nicole Rauch

June 3, 2013

slide-2
SLIDE 2

Our Starting Point

◮ Business Software ◮ Very poor code quality ◮ Planned changes for a module:

◮ Bugfixing ◮ new features ◮ better tests

⇒ A restructuring was required

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-3
SLIDE 3

The Domain

◮ Financial mathematical software ◮ Calculate for a bank account for each month:

◮ Balance at the last day of the month (ultimo) ◮ Average balance of the month Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-4
SLIDE 4

Problems of the Existing Code Structure

◮ Code writes values into separate data objects („Push“) ◮ Multiple writing operations for one value ◮ Parts of the code access previously written values ◮ Code is driven by the view from the inside: What do I need to

do in summary to be able to deliver a set of result values?

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-5
SLIDE 5

Problems of the Existing Code Structure

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-6
SLIDE 6

Our Approach

◮ Assumptions:

◮ The old code is mostly incorrect and incomplete ◮ There is plenty of knowledge regarding the business logic

◮ Decision:

◮ Greenfield reimplementation (with feature-toggle) ◮ Creation of new tests in parallel with the new definition of the

business logic

◮ Continuous adaption of the existing tests Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-7
SLIDE 7

Our Experience

◮ Problems:

◮ It was much more complicated and long-winding to specify the

business logic than we had expected

◮ Differences compared to the old code are hard to analyze ◮ The test coverage is too low

⇒ We miss relevant cases

◮ Causes:

◮ False assumptions ◮ We mixed restructuring and change ◮ Arrogance („We know things better than our predecessors“)

⇒ Our restructuring approach was much too naïve

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-8
SLIDE 8

How can this be improved?

◮ Gather facts in advance (assumptions are not sufficient)

◮ How much code coverage do we have? ◮ Which business cases are covered? ◮ Is there any specification that matches the code?

◮ If in doubt, the existing code shows the correct behaviour! ◮ Do not change the logic while restructuring! ◮ Explicit approval of the restructuring

◮ It must show identical behaviour (tests, bugs, features) Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-9
SLIDE 9

Goal

◮ Structure:

◮ Code mirrors the business logic

◮ View from the outside, driven by the expected results:

◮ Which values do I need? ◮ How is each value calculated? ◮ Which categories of results exist? Similarities, differences? Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-10
SLIDE 10

Goal

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-11
SLIDE 11

Ideal Approach

◮ Feature-toggle to compare the old and the new version

◮ Identification or creation of a minimal entry point to the

restructured area

◮ The API of this entry point must remain unchanged

◮ Important aspects of the restructuring:

◮ Driven by business logic ◮ Purely structural

◮ Technical goal:

◮ Separation of Concerns ◮ On-demand-calculation of all values („Pull“) ◮ Value caching via lazy initialization Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-12
SLIDE 12

Workshop

Workshop

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-13
SLIDE 13

Balance

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-14
SLIDE 14

Initial Average

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-15
SLIDE 15

Final Average

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”

slide-16
SLIDE 16

Thank you!

Code & slides at GitHub:

https://github.com/NicoleRauch/RefactoringLegacyCode

Andreas Leidig

E-Mail andreas.leidig@msg-gillardon.de Twitter @leiderleider

Nicole Rauch

E-Mail nicole.rauch@msg-gillardon.de Twitter @NicoleRauch

Andreas Leidig, Nicole Rauch Restructuring Code: From “Push” To “Pull”