TDD is Hard

Programming·Testing

TDD is hard, seriously hard. Perhaps that’s a bit of an overstatement, but for me, after years of “skill neglect”, it’s been a real struggle to pick up and get right.

The problem isn’t writing the tests, it’s writing code that’s testable. I’ve dabbled a bit with unit testing in the past but never really got the “unit” part of it. My tests would dive into a class that relied upon other classes and I would assert some value passed all the way through the call stack. In hindsight this is totally wrong (unless we’re talking functional testing) but at the time, my only concern was getting a passing test, which I was successfully achieving. It was testing, but not as I now know it.

So now I know better. Now I know that a unit test should only really concern itself with the class at hand. The results returned should be a direct product of the method called and should not have been meddled with by a third party class/library/what-have-you. Before really taking some time to learn the subject of testing, I would have looked at my testee, thought “Well this class relies upon this other class here, what can I do but let the call happen.” and as such my unit tests turned into functional tests. But now I know I can substitute those called classes out for fakes, dummies, stubs or mocks.

I started out by writing tests for some existing legacy code (TDD arse about face really) and really struggled with working out how to use my mocks as callees for my target testee. I was instantiating classes inside methods and then making calls out to it. There was no way of substituting the concrete class out for my mock. But now I know I can substitute classes using dependency injection.

The nice thing about mocks and dependency injection, is that you’re not just able to verify the result of your testee, you can also verify it’s interaction with the mock. If your testee is only supposed to call getAmazingValue() once, you can fail if the method is called more times than that, or perhaps even called with the wrong parameters. I would have taken it on almost blind faith that the interactions I had been creating were correct. But now I know I can record, replay and verify these interactions. *

The hardest part for me was writing code I could test. TDD forced me to think about the design of my classes all the time, every time. When doing TDD I start with the test. I think about how I would like the call to work and then I code up something just to make the test pass. Then I’ll think about how my class relies upon a call out to some other class. So I’ll create a small mock for the testee, go about injecting it to get the test working again and then make sure the interaction between the testee and mock happened exactly as it should of. TDD is a beautiful thing, but it has its thorns. Just like everything in computing, on the surface the concept is deceptively simple. But when you really start to look into it and try and do it properly, what seemed like just a piece of floating ice is really the tip of a huge iceberg. The pain of doing it right is well worth it with the reward of less bugs and easier changes. It’s a hard concept to keep going and feel like it’s worth it, as the benefits sometimes might not be seen for some time. It’s only in that moment of changing something that you see straight away that it’s broken other pieces of implementation, do you really feel glad that you made the effort.

  • While mocks allow for the testing of the interaction between the testee and the mock, this doesn’t always mean the relationship is correct. If I mock a class and expect a call to getAss() but the real concrete class expects a call to getArse(), then the whole thing falls apart when run in the wild. This might not be such a problem when the mock is derived from a common interface, but in dynamic languages, there is that margin for error.

It's an Improvement Adventure

Programming·Books Learning

It’s amazing how easy it is to be a programmer/developer/code monkey without having to do it properly. I got my first programming job when I was 17, a whole ten years ago, and now after recently reading Clean Code and Refactoring to Patterns, I can’t help but wonder what the hell I’ve been doing all this time. I’ve been writing code, but I sure as hell haven’t been programming. Looking at the code I’ve written as recently as only a few weeks ago now makes me cringe. “Why am I calling this method here?”, “What the hell is this comment?”, “Is this a joke?!?”.

I took a job as a developer for a mobile phone voicemail to email provider four years ago. I was only there for a few months but I still feel like I learnt more about programming while there then I had previously and ever have done since. I’m a firm believer in the notion that you’re only ever as good as the people around you. If you come in at a lower level you’ll soon pick up the pace, and if you come in over those around you, you’ll soon slow down to match your co-workers. As always there are exceptional circumstances. Sometimes if you’re too low, you’ll never be able to perform to the same standard, and if you’re too high, you probably won’t stick around long enough to get bogged down. I’m in a bit of a unique situation in my present role, because I work in pretty much complete isolation from other developers. Which means I have to set my own benchmark. I remember when I started, I was still so full of energy from my previous job (the one mentioned above) that I wrote some pretty cool code, which even impresses me when I look at it four years on. But as time went by I became more and more rusty and the quality of my code decreased dramatically. I stopped reading computing books altogether and took more of an interest in general business subjects. I took an interest again when Rails came on the scene, but none of the coolness or wow factor of doing Ruby could be applied to my day job doing Java, so that area of my life still continued to dwindle. The discovery of Grails changed that to a large degree, but by then I had lost the sparkle for Rails and my code became the same mess, just in a different language. I constantly think to myself that there has to be a better way. Rails and Grails felt like an answer to a certain degree, but the same underlying issues of design and brittleness remained. While features were quick to implement, changes were slow and painful to make. Simple requests became complicated hacks built upon other hacks to fill in holes left by other feature requests. Rails and Grails, while eye openers and moral boosters, didn’t solve my problems.

I was naive and blamed the language, the framework or the previous developer for my issues. I felt that my designs were sound and the code of a high standard. It “felt” tidy and the functionality all there, neatly sectioned off into different places. But Clean Code and Refactoring to Patterns showed me how wrong I had been. Regardless of the language or the framework, without well designed, robust and agile coding practices, you eventually hit a very hard brick wall. To say they were an eye opener is an understatement. It felt more like a programming re-birth. In hindsight, all the signs of bad code were there, I had just become totally blind to it.

Not very long after reading both books, I tasked with making some changes to an old legacy Java system. In the past I had absolutely detested working on it. Everything felt like a mess and that the slightest change would bring the whole system to its knees. But I felt confident this time. I started writing tests, which I had never done before, and started using the absolutely brilliant refactoring capabilities of IntelliJ. I spent a lot of time refactoring code so that the new functionality existed, wrapped up, in a state that it should, rather then in a shoehorned/hacked state. No doubt it too me longer then if I’d just shoved it in, but the system became better as a whole for it. The long reach of the masses of refactorings I performed have left parts in a greatly improved state. Perhaps in some cases, with added complexity - a small price to pay for better testability, design and extensibility. I would rather have an added degree of complexity over a brittle design any time

Anyone can knock out code, anyone can cut-and-paste a system together if they tried hard enough, but writing code alone is a solution to a small fraction of the overall problem. Anyone can churn out new features for a v1 release, but what about v1.1 or v2 or v3. As developers we dig our own graves. Clients don’t understand why things used to happen so fast, but now it’s sometimes weeks before anything new is ever shown. Fast - Slow - Slower.

This is where good, clean code starts to come into it’s own. Code that’s light on it’s feet. Code that’s easy to move in and out. Code that isn’t a brick wall and requires you to code around. Good design and good coding practices are at the heart of delivering a robust solution. If you are a developer you need to read the books I mentioned above, or at least just Clean Code. Heck, as a developer you need to read books. FULL STOP. I know many developers who don’t read at all, which to me is just unthinkable. You maybe one of the few that shrugs their shoulders after reading something like Clean Code and says “Yeah, I know all this already.”, and if so, good for you, but even you will probably know someone who could do with reading it, probably more then once ;)

"It works 60% of the time, all the time!"

Programming, Leadership·Management Rant

You can’t get a little bit pregnant. - Lou Mannheim

There’s a story about a manager that said his team was doing scrum/xp. When pressed as to the details of what that meant, the manager replied that they were doing ’no documentation’. Scrum is the in project management methodology of the moment. Quality? Scrum! Clear deliverables? Scrum! Happy developers? Scrum!

So yes, Scrum and what does it have to do with getting a little pregnant. My point is, if you’re going to do it, you need to go the whole way. It’s not enough to just dip your toes in the water. You may refer to your current load of work as a sprint, refer to units of work as stories and may even refer to your working capacity as your velocity. But, scrum is not about giving names to concepts, or about having a backlog, or having a burndown chart etc. Even if you did all of theses things together, you would still not be doing Scrum, because it is more then the sum of its parts. And I can guarantee you that if you start going down this path of faux Scrum, when things start to move slightly off course and panic starts to creep in, you’ll dip into your old ways to “fix” the immediate problems and then when things go wrong, you’ll give up on it completely and deem the whole exercise a failure. That’s why you can’t get a little bit pregnant. You’ve got to go all the way and get to fourth base.

“Woah, Joe, all theses companies are succeeding with Scrum, we should do it too!”, “Yeah lets!” - High five! The product backlog gets written up, the burndown chart gets drawn and every morning there’s a stand up meeting with some people to discuss ‘work’. A week or two goes by and it really starts to feel like the wheels are greased and progress is being made. Then an urgent client request pops up, a massive bug has been found, or that feature they asked for two months ago and which they’ve just seen (Hey, we’re retrofitting Scrum to primarily waterfall driven project), isn’t what they wanted. Well the request is fine, we’ll go through the current sprint’s backlog with them and they can decide what they would like pushed back. Oh, they don’t even know what a sprint is, “What did you say? Spinelog?”. Well the bug is not a big deal, we’ll just run our tests, make the amends and get it sorted real quick. Oh there aren’t any tests. Ok well at least the incorrect functionality we can handle, we’ll just plan it into the current or next sprint and continuous integration means they can monitor the progress as we go a long. Spontaneous combustion? Oh no. It’s not good enough to pretend, or for the development team to do it in isolation from the stakeholders or customers. To succeed you need buy-in across all the people involved with bringing the project to fruition. Everyone needs to understand the process and understand the role that they play. Pretending to do it ends up giving the team (mostly management) a false sense of security and that can really come round to bite you.

Don’t get me wrong, I’m not saying that doing Scrum in the above fashion is always wrong, because sometimes your circumstances leave you with little choice. But, what I am saying is that proclaiming to be doing Scrum without understanding it all the while living in blissful ignorance of the flaws of your own processes, is bad and harmful. I don’t mind mistakes or doing things the wrong way when you start out, because really you need to start walking the path before you can get to where want to be. I just can’t stand people saying their walking the path when really their just sitting around, biding their time.

Man looks into the Abyss, and there’s nothin’ staring back at him. At that moment, man finds his character, and that’s what keeps him out of the Abyss. - Lou Mannheim

Investing in Your Own IT - The Ultimate No Brainer

Leadership, Programming·Management Productivity Rant

I’ve been cursing my work computer all morning and am once again considering either bringing in my computer from home or just buying one outright (It wouldn’t be a first. I paid for my own monitor here at work. Go figure). Kris Kemper wrote a good post on the subject a few days ago.

I’ve seen this on every project I’ve been on. We are given slow machines, and time is lost. It may be lost because I’m running grep over a lot of files, it may be because when I have my all my development tools open and the machine slows down.

To me, when you’re in the business of developing software, investing in your own companies IT is a complete no brainer. Companies are normally extremely quick to spend on server hardware, but when it comes to development machines, spending is often few and far between. My machine here at work isn’t a “horrible” machine, but the agony it puts me through makes me feel that it’s perhaps not best suited to the task of developing on. But then even simple tasks seem to thwart it with constant disk grinding. I tried deleting an old repository checkout, no more then 200mb on the disk, and stopped it after it had only reached 14% done in 10 minutes. Virus checking my update of DirectoryOpus took nearly 3 minutes. Maybe it’s just a build issue and not so much a hardware one. Vista complains that all off the Office 2007 apps are not valid Win32 applications or the Snipping tool politely tells me it’s not working “right now”. Yes, I can see that. Whatever it is, it’s not the sort of thing that you need when you’re in the middle of something. Whatever, they should have got me a Mac.

I can understand that sometimes you just can’t afford whizzy machines or the latest version of software. Just don’t let me catch you running a .NET stack and complaining about not being able to afford machines fast enough to run it.

CSS Mastery

Programming·Css Books

I have always hated CSS simply because anything I ever did would break in either Firefox or IE. I would spend countless hours tweaking and experimenting to get things consistent, and even then it felt like things where hanging together by a thread. Once things worked, I prayed I never need to “change” it. I have to confess that up until recently I still believed that tables were the solution to most web UI problems. A table here or there never hurt anyone. Really though, tables are sooooo Altavista. Thus, I’ve been making a conscious effort to move towards styled divs without tables full of spaced out cells containing transparant spacer gifs. I have been pretty much fudging my way through it so I thought I should read up on the subject and saw that CSS Mastery had gotten some really good reviews. I stole the office copy, made my way through it and can honestly say, with hand on heart, that it is an absolute must read for anyone involved in any sort of web development. My CSS ability has developed by leaps and bounds. I recently put together our new site which gave me a chance to flex some of my new found CSS skills and it made such a difference. Everything came together quickly and painlessly. For the first time ever I had a site that worked in both Firefox and IE pretty much off the bat and I had a tidy HTML and CSS file. Will this book make you a CSS Master? Probably not, I’m not one yet, that’s for sure, but it will get you on the right track to eventually getting there.

To go off on a bit of a tangent. Some work places have a specific team that do all the styling and formatting of whatever the developers give them. Maybe on huge projects, it makes sense, but in small teams I see no benefit apart from massaging the developers own egos and beliefs that HTML/CSS is beneath them or something. This divided approach really bugs me, simply because I believe that it causes developers to not give the asthetic or functional side of what they produce any consideration. Un-usable? Hey, it works and that’s all that matters, it’s not my job to make it look pretty! The process is circular. Good design begets good functionality begets good design begets… You get the point. Design is not just about making things pretty, and that’s the trap that people fall into. It is about communication; giving the user an experience they understand and can relate to. I’m not a UI guru or claim to have all the answers, but I’m a big believer in at least trying. The curse of knowledge from the book Made To Stick plays a big factor in this. People find it extremely hard, if not impossible to imagine not knowing what they know, and that is what stifles communication. I once argued with a company director about an existing system’s poor UI. They claimed that it was sooooo easy and quick to use and I pointed out that’s because had been walked through it so many times and used it nearly every day. So eat your own dog food and do your own HTML/CSS. Use your own system and pretend like you’ve never used it before. Do a hallway test and ask someone to perform a task without telling them how to do it. You might be surprised by the results.