Refactoring Tests (or: How I Learned to Stop Worrying and Love the Red Bar)
When code is refactored, the tests associated with it help you to be sure that you haven’t broken anything.
But code doesn’t test the tests! If you’ve ever renamed a test and accidentally typed tsetThat... at the beginning, you have already experienced this.
So, here’s what I do to make refactoring tests safer. First of all I work out what element of behaviour my test defines:
public void testThatWhenSaleIsMadeReceiptIsPrintedWithAppropriateAmount() {
LoginScreen loginScreen = new LoginScreen();
loginScreen.enterLogin("Liz");
loginScreen.enterPassword("Pa55word");
loginScreen.clickOk();
SaleScreen saleScreen = new SaleScreen();
...
assertEquals(10.00, receiptPrintOut.getPrice());
}
and I break that behaviour by changing the code, maybe by adding an exception:
public class Receipt {
...
public void print() {
...
/** @todo remove this exception */
if (price == 10.00) { throw new Exception("Remove This"); }
...
}
...
}
then I refactor the test against the red bar!
public void testThatWhenSaleIsMadeReceiptIsPrintedWithAppropriateAmount() {
new LoginFixture().login();
ReceiptPrintOut receiptPrintOut = new SaleFixture().buyItem(9123456);
assertEquals(10.00, receiptPrintOut.getPrice());
}
As long as the test is failing because of my broken code (and not because of some null pointer which means that I’ve broken the test), it’s likely that it’s testing the right thing. When I remove the exception in the code, and see my test run green again, I have the same certainty that my test is still behaving itself as I do when I’m refactoring code. I can remove the exception at any time to check that my test still works.
For me, this technique has been particularly useful for those interminably long acceptance tests which have lots of duplicated functionality, or any complex change that my IDE couldn’t handle automatically. It’s also good for introducing mocks and stubs to tests; eg: introducing a stubbed data source instead of a real database, a custom output stream instead of a file, etc., as I know I won’t accidentally miss out the thing I was testing in the first place. I’ve also used it for refactoring an Ant build, to check that the patternset and classpaths I was using were still finding the tests.
It doesn’t always work, especially since acceptance tests frequently check more than one thing, but it helps.
Business as usual
So that was XP2005. What an amazing five days.
Thanks to everyone who ran tutorials and workshops, presented papers or took time out from the northern ale to chat to me in the bar, and everyone here who let me go. I learnt so much and had a lot of fun doing it.
Highlights for me included:
- Charlie Poole’s UI Testing tutorial, in which we learnt the how, when and all-important why of UI testing (sometimes you don’t have to)
- The Coder’s Dojo, in which we worked on the Game Of Life rules, and how to discount Harry Potter books
- Sitting outside on the grass, struggling with an out-of-sync clapping rhythm in the Music of Agile Software Development
- Laurent and Manu’s writing workshop, the amazing freewriting exercise and the feedback from the other attendees
- Learning how to sell XP to customers, and sharing some of the stories of pain and triumph
- The exhausting but entertaining exercise in The Drawing Carousel, and finally finishing the picture of Jack running for the beanstalk with a hen beneath his arm (Hubert Baumeister took pictures)
- Kent Beck’s closing speech on moving XP up a notch
- Feedback from new friends on pretty much every concept in my head, their patience in listening to me during various question sessions and their encouragement of my search for knowledge and understanding of Agile and XP.
I hope to run an Agile Haijin workshop at TW soon, in which we write haiku, learn about Eastern vs. Western ways of thinking, investigate parallels between Agile practices and haiku and learn to do The Simplest Thing Possible. Should be fun.
Freewriting
An exercise we’ve been practicing at the conference.
Sit down for ten minutes. Write. Don’t let your hand stop. If you can’t think of anything to write, write about that. Do this every day, if you possibly can.
In my first freewriting session, I wrote about what buildings would be like to live in if they had no roofs; moved on to stars, exoplanets and alien life, and finished by ranting about what would have happened if the dinosaurs had made it through to sentience.
In my second freewriting session, we added a constraint: every line had to start with “now”. I wrote two pages of rhyming shite that I am very glad I will never have to write again.
In my third freewriting session, I wrote a 588-word flash fiction story called “Yesterday’s Tree”; an idea which I had in my head for a while but have been agonizing over.
Just about to start the first voluntary session of my freewriting career. Wish me luck.
Greetings from XP2005
Lovely to finally meet Alan Francis and Dave Hoover for the first time.
Things I learnt today: I have a hard trouble shifting preconceived notions. (Fortunately Alan and Dave are similar to my expectations.) Whilst working through an exercise in which each of us was taking a turn to make a failing test pass, then write another (ping-ping-ping-ping programming?), I almost rewrote the class, very disrespectfully given the efforts my fellow attendees had gone to to get to that point. (Will link to The Coder’s Dojo and the Harry Potter exercise when I have a mo.)
I did eventually get the test to green-bar, but the others were right: I took far too big a step. There was a much smaller step that I could have taken which would have meant rewriting less code, but I didn’t see it. In my head, I had already decided how to solve the problem. If I had been given the code fresh, I might have come up with the smaller step, but this time round I missed it. So; kudos to me for getting the hardest test to pass, nil points for methodology – but at least I learnt something new. Now all I have to do is work out what to do about it…
Haiku, Agile and a haijin apprenticeship story
I received a letter yesterday morning telling me that one of the four poems I submitted to a competition had been selected to appear in the anthology.
Fantastic! But… it wasn’t one of the three carefully crafted, heartfelt or witty pieces I had thought might stand a chance. It was a throwaway nine-word haiku called “January Blues”, intended to capture that moment of sadness as you take the Christmas tree down.
I need to have more respect for my ‘ku. I stopped writing poetry at fourteen, when I realised how bad it was (teenage goth poetry is far worse than that of any Vogon). I didn’t take it up again until a few years ago, when I ran into the Scifaiku group – some of the most talented individuals it has ever been my privilege to work with. Haiku and science-fiction haiku changed the way that I thought about poetry, and my poetry changed too. Some of it’s even good, I’m told.
As my brain was turning over last night before I slept, I realised that there was a reason why the competition judges had chosen this haiku over the other poems. They, too, identified with the moment. A haiku is intended to capture an experience, often something common to a culture, and distil it into a form that makes that shared experience happen all over again. It takes subliminal information – things which everyone knows about, but no one talks about – and puts it out into the world so that everyone recognises that the moment isn’t something particular to one individual, but an experience that the poet, too, has had.
I wrote a while ago about code comments, and how they should be haikuesque in their delivery of information – but it goes further than that. Agile itself is haikuesque. It takes information which is understood at an unspoken level and makes it obvious. With a haiku, someone might say, “Wow! You feel the same way as me about the end of Christmas!” Whereas with Agile, someone might say, “Wow! You think that the persistence layer is really hard to work with too!” The haiku is rewritten in careful calligraphy beneath a little oriental watercolour. framed and put on the wall. The persistence layer becomes the subject of a discussion with a view to improving it.
As an example, here’s a shared experience that you might be familiar with, and which Agile makes explicit. Reading or writing a ‘ku feels very similar.
code falls into place -
green bar!
XP 2005, Sheffield, UK, this weekend
I will be there. If you’re there too, and anything I’ve written makes you feel like talking to me – even if it’s to tell me that Vogons write better poetry than I do – say Hi. I will be female, wearing glasses and sporting long red hair.
The apprenticeship discussion
Thanks to Dave for clarifying, again, his concept of an apprentice as someone who’s been working in Software Development for less than five years.
I think that even within my first five years of software development, there were levels of promotion which may have been less clear-cut than a move into management. Becoming a software designer, learning to write customer requirements and convert them into acceptance tests, and mentoring junior developers were roles which I took on during this apprenticeship as I felt ready for them. So I understand and agree with Resist the Promotion, but I still think it’s important to recognise your own growth and take the next steps as they come to you.
Dave, you don’t have comments turned on in your blog, so I’m putting this here. A couple of phrases in your post gave me the impression that Resist the Promotion vs Step On Up might have annoyed you; if so, I apologise. My image of you and Pat as masters was based on my perception of your blogs, and your lovely website, not through any personal knowledge of you or your client work. As ever, perception is an interesting phenomenon, and again I learn something that should have been common sense: just because people know things that you don’t doesn’t mean they’re more experienced than you are.
Resist the Promotion vs Step On Up
Reading the discussions between Patrick Morrison and Dave Hoover on Dave’s pattern, Resist the Promotion (here and here), I hereby declare them to both be right and propose a new pattern, Step On Up.
This is what I think I’m doing by becoming an Agile Coach.
Dave says that he’s aiming his patterns at new programmers, but I’ve found that a great deal of them are applicable to where I am in my career. I don’t feel as though I’ve finished my apprenticeship yet, despite having over seven years of industry experience. Maybe they’re applicable at various stages of apprenticeship. I’ve recently come to realise that there’s a whole side to software development which involves communication, accurate feedback, customer interaction and other elements which have more to do with psychology than computing. Understanding the process which goes on in the minds of those involved in software production has to be an important part of software development. I accept that it’s important to understand the technical aspects of the job in depth, and therefore resisting a promotion is important, but I feel it’s equally important to recognise the point at which you as a developer can move on to help facilitate the process which turns ideas into reality.
The difference, I think, between the way Pat and Dave describe the transition to management is that Dave’s concept of promotion is something which someone else decides you’re ready for, whereas Patrick describes managers who really are ready. They’ve moved naturally into roles for which they’re suited, and as a result have redefined their value to the team. It’s very tempting to think that these managers, with their perfect lives and perfect facilitating abilities, have come to the end of their apprenticeships and are now considered to be masters. I don’t think that ever happens. They’ve just taken a natural step which has pushed them on to another level of learning, beyond the ken of us mere mortals.
At the end of the day, the universe is a highly intertwined place. Unless you’re God, you have no way of becoming master of the many interactions and influences which affect your project. Unless you’re a Buddhist who’s achieved enlightenment, I very much doubt that you’re master of your own head. In the same way that an elderly monk might teach a young novice, yet never cease seeking enlightenment himself, I can’t imagine a master who ever stops trying to master his craft. Apprenticeship has to be a life-long pursuit; a journey towards an unreachable destination, but an important journey nontheless.
Dave and Pat, if you read this; I’d be interested to know if this makes any sense to you. From my point of view, you’re masters. Do you consider yourselves to have finished your apprenticeships yet?
Reading Esther Derby’s article on peer to peer feedback made me remember something that happened a few years ago (not here, I hasten to add). I’m sharing this as an example of how peer to peer feedback can resolve a situation, so that any women out there who are experiencing something similar might take courage and do something about it, and because maybe it might give any men out there who indulge in this kind of behaviour an idea of what it’s like to be on the other side. This, gentlemen, is why the sexual harassment and discrimination laws were invented, and why so many women end up in court and the front page of the Times.
The company had a fairly laddish, alcohol-fuelled culture with only a few women (about 10%). The lines of ‘appropriate behaviour’ were somewhat blurred. If you’re female, have attractive qualities, and want to know what it’s like to work in a company with this kind of culture, go down to your local next time England are playing on a Saturday afternoon and take a seat. Hang around after the game. Most of the attention will be on the football, just as most of the company got on with their work, but there will be the occasional innuendo or flirtatious comment that may not be entirely unwelcome. Plenty of women thrive on attention. I certainly enjoyed my time with the company, and was sorry to leave the many friends I had made there.
However, sometimes blurred lines get crossed.
We had a secure project in the basement with no network access. An office upstairs had been set aside as a “daylight room”; a place where people could come to check their mail, browse the web and stare out of the window. I was off-project, and had been assigned a spare computer there to get on with training. So there I was, trying to concentrate, while across the way my colleagues were trying to guess the cup sizes of various women whose unclad attributes were displayed across the 19 inch monitor.
Now, I’m not easily shocked, and I could probably have ignored it, or just asked, “Honestly – do you have nothing better to do with your time?” But there was another aspect to this that stopped me from saying anything – something which made me sit as quietly as I could, blushing to the roots of my hair, and trying to bury myself in my work. The three gentlemen who were standing up behind the one colleague I knew were staring across at me, whispering amongst themselves, gesturing and giggling like schoolboys; applying their imaginations in the spirit of the competition to the only real-life example in the room.
I had three options. I could have stayed quiet and ignored it, and hoped it didn’t happen again, or maybe gone out and made a cup of tea till they moved on. I could have taken my problem to the senior management, or to the MD, both of whom were quite laddish themselves but understood the lines better than the younger company staff. The option I chose was the most difficult, and I only did it because – well, did you read my apprenticeship story? The rather scathing review that I got in my earlier years regarding the quality of my code? No one gave that review to me to my face, and finding out six months later hurt. Like a physical blow. It said more about people’s ability to talk to me, or their care for my work and professional well-being, than it did about my code. This was only a month or so after the review, and it was very fresh in my mind. So I took the third option, and caught the one colleague I knew (let’s call him Ted; not his real name) as he was leaving the room.
“Ted, can you spare a couple of minutes?”
“Sure. What’s up?”
“It’s the material you were looking at over lunch. I’d prefer it if you didn’t do that again. It’s distracting, and it made me feel very uncomfortable.”
“Oh. Um, okay. I didn’t think you’d mind; you’re pretty open-minded about that kind of thing. I’ve never seen it bother you before; we were just having a laugh.”
“Ted, do you know what your friends were doing while you were browsing the site?”
He didn’t. I told him. Blushing furiously.
“Oh. God, Liz, I’m so sorry. You must have been really embarassed. I had no idea…”
“No, I could tell. Ted, you need to know that this was hard for me. I nearly went to the MD instead of talking to you, because I feel embarassed just confronting you about this. Maybe there are other women in this company who’d do that. You shouldn’t make assumptions about how people will react to that kind of content, or how it will make them feel.”
“Liz, thank you. I’m glad that you told me; I can tell that it was difficult for you, but I really appreciate it. I’ll talk to the others and make sure it doesn’t happen again.”
We got on just fine after that.
I can’t actually imagine a more difficult and embarassing situation to be in than that one (except those incidents at office parties that are problems entirely of our own making!), but in retrospect I’m really glad I dealt with it this way. It’s given me the confidence to take people aside when I need to. I try not to let third parties deliver my messages any more. Because of this new confidence, I was able to help a developer I was mentoring become aware of his mistakes and improve his coding techniques well before his review. I was able to tell a colleague that smoking cigars is not better for you than cigarettes, and they smell far worse. I was able to tell the team leader who gave me the poor review about my frustration with getting the feedback on my code so late. Before I left, I also had a frank, open-minded and constructive discussion with a senior manager regarding the company culture and the problems it presented to female staff. And I told him this story as I’ve just told it to you.
Small world
Occasionally, getting together with my colleagues from across the world, I’m suddenly reminded of how small the Earth is, and how many gulfs and divisions there are on this little planet.
Countries represented at our UK Away Day on Saturday included: US, Canada, Australia, China, India, South Africa, New Zealand, Brazil and no doubt a dozen more. We don’t have anyone (AFAIK) from Zimbabwe, Iraq, Cambodia, Afghanistan, Palestine or any of the other countries where things are ‘difficult’, and perhaps less westernised (by which I mean having access to decent sanitation, healthcare, world-wide communication systems like telephone and the internet, roads, railways, a stable economy etc. – the things which need to exist for a country to be part of a global community. I have strong opinions on morality, fundamentalist religion and various systems of government, but I don’t mean to bring them into this post.)
I’d love to see the world made whole; for people from every country to be working with people from every other. There will always be ‘difficulties’ wherever there’s a difference of opinion, but there are so many challenges thrown at us by nature that it seems a waste for us to be making our own. I’d love to see this happen within my lifetime. It seems as though it should be possible.
If the world started working together, we’d finally be able to concentrate on the real issues, like sorting out Earth’s environment and colonising Mars. If humanity isn’t going to be a blip in the cosmos then we’ve got to get off planet at some point in the future. It’ll take generations to achieve. It’ll take some brave pioneers, and I don’t doubt that people will die trying. It’ll mean taking technology to limits that we can imagine, but not yet realise. It’s going to be exciting, and fun, and scary. I won’t be around to see it, but I can see the start of it here in my own office; we’ve just been introduced to three gentlemen who are joining our client from India.
The world is getting smaller. I wonder how long it will be before we decide that it’s not big enough any more.


Comments