Skip to main content

Posts

Showing posts from May, 2010

Are mocks/fakes reusuable?

Programming 101 states:
Don't copy and paste code. If you find yourself doing something repetitive then do it right so you can reuse the same code. Functions, classes and even separate files all serve this end.

Now that I'm writing tests all the time I often find myself creating Mocks. Mocks are where you tell code to use a pretend version of some functionality instead of the real one. It could be because the real one does something you don't want in your tests (writes files, reads a database) or it could be that you've got some messy legacy code you can't to pull into your tests (yet). There's other reasons too but you get the idea.

So if I make a Mock version of a class it makes sense to try and share that with everyone else that might be trying to test with that same class. Or does it?

That assumption has some serious flaws that I'm only now starting to understand. And here's a few:

Behaviour you need to test may be completely different to the next guy

So …

Just one more X

What is it about human psychology that makes this such an effective way to keep people paying attention. It's the most obvious in games. There's always 3 or more short term goals you're aiming towards. Just this one fight. Just until the next save point. Just until I get 10 of this item.

It works for other things. Tasks at work (go on, admit it!). Novels with short chapters.

Is it possible to transfer this fantastic power to other things in life? Cleaning the house (just dust one more cupboard). Exercise (just one more lap). Often not. Why is this?

The human brain is a stupid but fascinating thing