Tag Archives: Disciplined Agile

Refactoring: strategy & collaborative work

Introduction –  The extended definition of Refactoring contains also this part:  “Its heart is a series of small behavior preserving transformations. Each transformation (called a “refactoring”) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.”- Martin Fowler, at refactoring.com.

Note: this imaginary dialog it’s inspired from an intensive and extensive practice.

What if we will have to perform a Big Refactoring? 

Refactoring cannot be big, it is a special kind of redesign, performed in small steps. See the above definition again.

Reformulate: I need to do a lot of refactorings to clean a legacy code.  There is any best practice?    

You need a strategy?

Yes!

Well, agile and software engineering offer a lot of tactics, including the Martin Fowler refactoring sets but almost no strategy, except … you should work clean from the first time. Use Martin Fowler indications from the start (or early enough), also Uncle Bob Clean Code and Clean Architecture.

Hey! I already have a lot of legacy code debt to solve!

Ok! Let’s build a strategy: how do we start?

This was my question!

Refactoring supposed improving the design, while preserving the functionality. Tests included. Do you have good requirements specifications or a good set of automated tests?

Not in this case.

Then you should recover functionality knowledge from the code and put/perform incrementally some tests. Better: functionality should be explicitly represented in the code and should be testable. And remember: there are two kinds of functionality…

Two?

Yes, first, the one that is application-independent and represent the target domain (domain business rules) and the one that is application-depend aka flow of control.

I remember: that sound like Uncle Bob Clean Architecture.

Yes. You will need to be able to apply distinct tests to them, without mix them with other concerns such as UI, persistence, network and others. Anyway, where do I usually start? I will try to make the running scenarios very clear into the code and that mean the flow of control.

In English, please?

I want to clearly see these: were the event triggered by the system actors start and the end-to-end full path until return. More, I want to refactor to make this path clear enough.

How could be not clear? ­­­

Global context. If the functionality path chaotically accesses the global context, then we could have undesired intersections with other paths/scenarios, that will compromise both data and function. In the same time, we can decouple flow/orchestration from specialized concerns.

What we get?

We will have explicit representation of the functionality (with no undesired contacts with other flows), needed for tests (we can apply auto-tests on it). Also we will have the first entry points to the specialized parts that also could be <decorated> with some tests. Then we can apply tactical refactoring as we need.

And …the domain business rules?

Must be decoupled from other concerns and you have to dedicate them specialized design/test elements.

That’s all?

Almost. You need to test any redesign. Tests need knowledge about functionality. If some parts are missing, now it is the time to recover them in auto-tests (preferable) or in other form of specification.

How do I know that recovered requirements are correct?

You don’t. More, you should always suspect that spaghetti-like legacy code include many unobserved bugs. You should validate these functional requirements by intensive collaboration with your colleagues, with domain experts, customer and other stakeholders.

Do you have any idea about how to do that?    

Start with Pair Programming (refactor in pairs). Pairing is not enough, and you will probably need more people involved – use Model Storming: discuss the resulted functionality with more colleagues.

Model Storming?

Yes, it is an agile practice, part of Agile Modeling (and Disciplined Agile) and it was created to complement core practices from XP. Also, you should actively involve your stakeholders in validating the recovered functionality…. Active Stakeholder Participation, that it is another Agile Modeling recommended practices. And at the end you will have more free bonuses.

What bonuses?

Functionality it is easy to accurately read from code (seconds!) and your colleagues and your stakeholders will already have acquired the recovered functional knowledge.

Summary –  Refactoring for significant spaghetti legacy code need tests/testing. Usually, knowledge about functionality necessary for testing it is insufficient, so must be recovered from the code. An effective & proven way to do that is to apply Clean Architecture principles: decuple both domain rules and application specific flow of control (aka use cases). Anyway, legacy code with too much technical debt will contain a lot of bugs, so recovered functionality it is inaccurate and need to be validated.  Knowledge & expertise needed for validation it is distributed among team members, domain experts, customers and other stakeholders, so you need to work in a collaborative manner with all mentioned parts. There are some outstanding software engineering and agile practices that could help on this aspect:

Note: “need” and “necessary” are often use in above text, just because we have followed the logical path of necessary things for testing a redesigned legacy code.

Remember: A lot of technical debt ~ inaccurate functionality. To refactor & test, you must re-start the process & collaborative work from functional requirements acquisition.     

New product and Minimum Viable Process with DAD

A live ecosystem

With a new product we will initiate and we must keep “alive” a full ecosystem (see previous post “A product … is not a product“) that involve:

  • business relationships
  • a viable process economics
  • sustainable technical solutions
  • a product team(s)
  • repeatable results
  • an evolving consumable solution and others.

What help me and my collaborators in some real cases were Disciplined Agile practices and some complementary ones such Clean Code and Clean Architecture.

“Temptations” and consequences

The first temptations on building new products were we have observed undesired consequences:

  • Copy-Paste Process: just repeat – without sufficient adaptions – the process from the existing products (more or less similar with the new one). The pressure could compromise more this process “re-usage”.
  • Ad hoc process:  If an existing process is not available, an ad hoc process is adopted without sufficient discipline and many process goals are neglected
  • “Normal” life-cycle: direct re-use a “normal” project life cycle, standard for the past projects/products
  • Ad hoc team: superficial building of product team, without sufficient collaboration and skills

The consequences could be very unpleasant:

  • A fragile product, where the poor design does not allow to reach the opportunities:  cannot deliver required changes, cannot offer a sufficient quality
  • The product is not consumable: not enough user level guidance, integration problems, performance problems
  • A significant mismatch between offered features and customers’ needs

 Different product – different ecosystem

We cannot re-use the process of a previous product as it is – the elements of the necessary ecosystem could vary more or less, and here are some elements that we found to be different:

  • The inherent complexity of business domain or of the solution
  • The customers & associated relationships
  • Derived from complexity: team skills, minimum viable good design
  • Required quality
  • Default performance
  • Others

As a first conclusion – we need to inspect and investigate the new context and adapt our process in order to build the proper ecosystem. Context investigation and adapting the process must be some permanent concerns, but the expected big deviation introduced by a new product must tell us that this is a special case to be considered.

 Risks, incertitude and opportunities

New product means a higher degree of risks, incertitude, but also possible opportunities. Risks and incertitude must be addressed in order to protect the opportunities and in the same time we need to keep the process adaptive enough to respond to these opportunities.

The main question is what practices and approaches we will need in a such context? We have found some that works.

Incertitude adapted life-cycle

For a new product, it is less likely to have a good enough initial envisioning of the requirements (and of the solution).  The DAD option of exploratory lean startup life-cycle propose a more adaptive approach where initial idea could be repeatedly adjusted after getting business domain feedback about what we incrementally build.

Choosing the right process options

As I have mentioned, the process “deviation” from what we know could be significant. Anyway, the process goals will be similar, but is possible to be needed to choose some different options. DAD main logic was built exactly on this idea. Beyond life-cycle approach we can choose various options for goals such: technical strategy, prioritization stakeholder’s needs, requirements elicitation methods, prove architecture early, validate release.

Some examples of options per goals described by DAD guidance:

  • Architectural spikes and/or end-to-end skeleton for proving the architecture early
  • Business values, Risks, dependencies as criteria for prioritization stakeholder’s needs
  • Coaching, mentoring, training, pair programming for Improving team members skills

When we begin a new product: best time to start adapting our options to the new context.

Effective practices

Building a new product it is difficult endeavor from many points of views: requirements clarification, solution design, (new) knowledge management. Here some key approaches and associated practices that we have found to be successful/helpful.

Effective/Efficient collaboration – apply Non-solo Development for modeling (Model Storming) and programming (Pair Programming). Active Stakeholders Participation.

  • Non-Solo Work it is critical for high difficult tasks related with initiating a new product
  • Because knowledge is just newly created we need to efficient/effective distribute that knowledge to the other team members and to the stakeholders

Opportunistic envisioning – Look Ahead

  • We cannot solve complexity and incertitude only with normal milestone-based forms of looking ahead – Inception Envisioning and Iteration Modeling. It was very useful to opportunistically use Look Ahead Modeling.

Knowledge evolution – Document Continuously, Document Late

  • Knowledge is just created – we have suffered when we forgot to capture, at least the essential – Document Continuously help us on this aspect
  • From time to time, we have extracted later some overview information (System Metaphor, Architecture Handbook), when our view and results has been stabilized – Document Late

Adaptive process –now is the best time to do that

  • The process was rather fluid and was defined/clarified as we advanced and saw what really works in the new context.
  • Good options were demonstrated by
  • Do Just barely good enough and refine not only your work, but also your process in rolling wave. Use events as architecture envisioning, look ahead modeling, iteration modeling to adapt your process

Minimal good design

When a new product was started, before and after the Minimum Viable Product, we were forced to keep change the product quick and with a sustainable pace. In order to “chase” the opportunities, we had to pay legacy technical debt and avoid as much is possible a new one. A minimal good design, it is necessary to deal with this continuous changes in the case of context complexity.  In this case, Refactoring,  Clean Code, Clean Architecture practices were the backbone for an agile, adaptive design and finally of an adaptive product.

Each distinct product will need a specific Minimal Good Design!

Some conclusions

DAD is built to offer context-adapting guidance. Using Disciplined Agile and outstanding practices as Clean Code and Clean Architecture it was be what we need to build a new product and to define the Minimum Viable Process necessary for that.

DAD and Agile Modeling practices are described here.

Bonus!

Just in time and Envisioning versus complexity

Principles for reducing complexity:

Apply envisioning for accessible information and just-in-time for what is far from certainty.

That is matching with the logic of Stacey Matrix.  Just-in-time is not (always) a silver bullet.  We need process approaches that effectively cover both dimensions #agilemodeling #DAD

Before DevOps: Delivering more by delivering less

Undesired complexity give undesired deliveries

There are many aspects to consider related to DevOps and delivery, many strategies and (in the right context and the right approach) there are also some very useful tools. It is a complex issue and last thing that we want is to make that even harder because of undesired complexity. A poor and an un-appropriate design is the main cause of the problems.

Suppose that the main aspects of a product functionalities and design are the followings:

  • Functionalities: f1, f2, f3
  • Business rules: r1, r2
  • External frameworks, technologies, and drivers (APIs, hardware interfaces, others) e1, e2
  • External interfaces: i1, i2,
  • Technical mechanisms: t1, t2, t3..

Should stay together only what it changes and should be deliver together.

As generic rule, if one of this aspects it is changed, we do not want to affect, change and deliver others aspects. Below are some examples of anti-patterns.

Anti-pattern: Lack of functional cohesion

Just a start example: If was requested to deliver a change for the functionality f1, it is highly undesired to change also functionalities f3 and f4 only because f1, f3 and f4 are unnecessary coupled in the implementation. This coupling could mean: same function, same class or same package as opposite of using distinct design elements.

Anti-pattern: Non-DRY business rules

If the business rule r1 it is used in more functionalities (that have dedicated components) and that rule implementation is duplicated (multiplied) in every functionality, then a change in r1 will require change and delivery for all those functionalities and components.

Anti-pattern: Mixing functionalities with external communication

Suppose that f1 and f2 are using TCP sockets for communication with some external systems and we need to replace this type of communication, if the f1, f2 implementation is mixed with socket management aspects, then we need to change and deliver also f1 and f2.

Anti-pattern: Mixing functionalities with external interfaces

External interfaces could suppose handling of some specific external data structures and/or of some specific communication protocol. If we are mixing these parts with internal functionalities, with any change in the interface, we should change and deliver also some not related internal parts, .

Anti-pattern: Mixing functionalities with technical aspects

You need to protect the representation of the business inside the product from the changes related to technology. Where is the technology involved? Any I/O aspect that wrap the hardware: GUI, network, databases, file systems is strongly related to technologies and platforms. If, for example, you have a lot of functionality and business representation in the design of the GUI elements, any change in GUI technologies will affect the business representation inside the product, that should also massively changed and then delivered.

Anti-pattern: “Utils”

A symptom of a poor design that could cause undesired supplementary work on development and delivery are the “utils” packages, especially without clearly dedicated sub-packages. Examples:

  • “utils” package that mix together classes belong to different functional aspects
  • “utils” package that mix various technical aspects without having dedicated sub-packages

This kind of design suppose that we need to change, test and deliver the “utils” almost with any change of the product.

Anti-pattern: Dirty code

Breaking simplest Clean Code rules (similar with the ones that are used in refactoring) could cause undesired coupling and undesired increase of delivery scope. Some examples that could induce such problems:

  • Any duplicate/multiplied code
  • Breaking SRP principle
  • Global context data, global variables (breaking Law of Demeter)

Minimizing the need of change and delivery

We need to reduce the need of change and delivery only to the required ones. There are several practices and approaches that could avoid that problem (the examples presented above or similar ones):

  • Keep the code clean by writhing Clean Code first and refactor and pay the technical debt whenever it is necessary
  • Use functional cohesion as the main criteria of creating components and packages
  • Use Clean Architecture that propose a default, strategic separation of concerns
  • Extend XP engineering practices (Simple Design, Refactoring, TDD) with the ones from Agile Modeling and DAD
  • Respect Law of Demeter on any level of the design – do not use any global context
  • Make the products adaptive by keeping up-to-date with target business by often and early injection of feedback from that business.

You can argue that I already write about all these in a previous post “Roadmap to an Agile Design”. Yes, indeed, in order to deliver just what it is needed and avoid waste you must have an Adaptive Design, the main characteristic of an Agile Design.

In order to that you should be open  to all outstanding agile practices for design and do not be closed in the smaller universe of some very lightweight Agile methods. And remember that those lightweight methods are not created as full process methodologies, but as indications to build a customized process.

Imagine that all these problems are far worse if we have multiple variations and variants of the same product. The massive effort for any change and delivery will block the overall agility and responsiveness of the development team and massively reduce the overall economics for both development and customer side.

Use a strategic separation of concerns

Do not use any reference to any global context

%d bloggers like this: