Sunday 18 November 2012

Exploratory Refactoring - Understanding Legacy Code

Remember how Neo can see people and places and events behind the code? Well. Not all of us mortals can see the shape of things at first glance of code. Actually, "He's the One".

Martin Fowler suggests refactoring code when trying to understand it. Also Michael Feathers describes lots of techniques to work with legacy code. This post is about step zero: understanding the legacy code.

Exploratory Refactoring is the act of applying a series of refactorings to legacy code merely to understand what it does. That's it. There is no magic here. After you understood the inner workings of the code in question, you typically throw away all of that refactoring and start to really and seriously work on the code. With end-to-end tests, unittests and the whole shebang. But I'm getting ahead of myself.
I typically start off with simple refactorings, like Extract MethodDecomposing Conditional, Rename Method, Rename Variable, Introduce Explaining Variable. This phase is like when an archeologist hand-cleans stuff during excavation. She might already have ideas of what each part is, was, but that's not the point. In this phase she just wants to make the parts as apparent as possible.
In our case however, you don't have to be very careful - as this is a throw-away refactoring - in extreme cases you could just pseudo refactor the code. However the closer you get to real-life refactoring, the more you can learn from it. You will get to see unexpected inter-dependencies, which  can influence your real refactoring later on.
Once you have done that, you can start to fit the pieces together and you can continue to dig deeper - or go higher in terms of abstraction  - , and use more complex refactorings which help you to unveil the intrinsic structure of the code, like Consolidate Duplicate Conditional Fragments, Remove Control Flag, Split Loop, Reverse Conditional. It could be that after quite a few refactoring, you still don't see structure or intention: Worry not! You will see it soon. Just keep on factoring each small bit of information back to the code.

One last bit of advice:

Don't make the typical mistake of dissing and cussing the author(or the poet we like to call them) of the legacy code. Not because it's simply bad manners, and not because we all have legacy skeletons in our careers  closets, but more importantly, because that way you distance yourself from the poet of that code, making it that much harder to see what he meant to say, when he for example named a variable $last_element
(Whilst if you try to indentify with him, you might realize, he really meant $previous_element )

Read full post...

Wednesday 27 June 2012

Dan North misses his own point?

Dan North is one of my favorite tech speakers. He is funny and entertaining as well as thoughtful and subversive. He gave a speech at NDCOslo about Embracing Uncertainty. He states that risk has two important aspects: Probability and Impact. Then he observes very astutely how most of us in IT focus only on minimizing probability and why that focus is misdirected. His reasoning is that you can never decrease the probability to zero so you should focus on minimizing the impact. It's a typical Dan North observation: almost embarrassingly simple yet very refreshing and eye-opening, which puts conventional methods into new perspective. This kind of ideas are the very reason I like him so much. Then he goes on to describe a few techniques with the help of which we are supposed to embrace the uncertainity of effort and uncertainty of technology and so on.

The first technique is called Real Option where you treat everything as a financial option, so that the later you make a commitment the better chance you have to make the best decision. You notice the word 'chance' in the last sentence. This method is very similar to the Last Responsible Moment and one that you should use, but it's optimizing for probability and not impact.

The other one is called Deliberate Learning where you try to find the areas in which you might be subject to second degree ignorance, in other words areas where you don't even know what you don't know. He emphasizes that there will always be stuff we don't even know to be unknown, yet he wants us to try to know anyway. Again, in my opinion this technique is aiming at minimizing the probability of things going awry, and not the impact of a possible mishap.

He's picking a little on eXtreme Programming in this very talk for trying to embrace change instead of uncertainty. I find it ironic because many of the XP practices and principles are about minimizing impact. The Iterations, Incremental Design, User Stories, Baby Steps, Short Release Cycles, Done Done. All of these are based on the simple idea that the smaller you can make the completed chunks of your product the smaller impact of any possible mistakes will be.

Read full post...

Thursday 5 April 2012

Root-Cause

Root cause analysis is one of my favorite XP practices. Because it's so simple and so powerful at the same time. That said I very rarely use it. Or do I? It turned out that I might use it without knowing it, as the below described quick series of questions and conclusions sure do look like a root cause analysis after all.

We had the iteration demo yesterday morning and the very first story simply was not working. At all. We thought it was but it wasn't. We took our loss of 2 story points in stride and moved on with the demo, then with the retro and then the planning, then the team started to work on the tasks. In the evening when I got out of a long meeting the red bug-card was still on the board with a red magnet on it while many new tasks were already completed, one full story even. I knew something was fundamentally wrong I just didn't know what.

As luck would have it, ten minutes later I called my mentor/business partner and told him how much we need to work on those values were just discussing in that long meeting
because in our earlier projects people had to be stopped from debugging code during the demo.
So then we started to discuss how did we get here.

Why did nobody care this time?
The level of engagement/involvement of team members is very, very low.
(Why?)
The project scope is changing every week. Stories, epics get dropped, new ones spring to life. It's unclear what percentage of the project we have completed, and what is still to go.
(Why?)
The release plan became a living document in the extreme: due to unexpectedly low velocity and ever changing requirements and underestimated stories, the scope was negotiated and re-negotiated over and over in the name of agility.
(Why)
Changing scope, estimating new stories on the fly seemed like the obvious solution when the estimates turned out be way off.
(Why)
Not the whole team was involved in the release planning. This is also the reason of their lack of engagement.

The 'why's are in parenthesis because we didn't actually ask them. We were just discussing the important aspects of the past couple of months.

How we are planning to turn all this around is the subject of the upcoming posts.

Read full post...