Monday, 22 June 2009

Doing it for yourself - deciding whether to use someone else's code

[caption id="" align="alignleft" width="300" caption="Photo by jurvetson"]Photo by jurvetson[/caption]

Using someone else's code can be great.  Or it can be horrible.  If it does exactly what you need it to, doing so with no bugs and no ambiguity, then this is awesome.  If you need to make changes to buggy, uncommented code written by someone who thinks the GOTO statement "really isn't that bad", then we don't envy you one bit.

So, here are some of the things you need to consider when deciding whether or not to use someone else's code.

Does it do what you need it to do?
The most basic question you can ask.  If the answer is "no", then this code is probably not useful to you.  The only exception might be if it almost does the right thing.  If you can make a relatively small set of changes to code that you understand (or that you can figure out), then it might be a better plan than writing your own code (which is presumably the alternative), in terms of the amount of time and effort it will require.  Remember to factor time for testing and debugging into this - if you're going to modify someone else's code, you need to be very sure that you've really changed it in the way you think you have.

Is their code any good?
Do you trust it?  Is it well-written?  Are there tests (or test results) that you can access to confirm whether it works or not.  Code comes in all flavours from "Dear lord, this code is amazing" to "I'm going to hunt down the person who wrote this and make them eat their desktop" and you need to be aware of where on the spectrum their code is.  The reason for this is simple: it tells you how much time it will take you to put it into a use-able state.  If you're really lucky, the code already does exactly what you require and it's reliable.  In this case, the amount of work you have to do is close to zero.  The more testing, modification, refactoring to improve the code base etc you have to do, the more time and effort it will take to produce code you can actually use.  And at some point, this amount of work will exceed that required to simply write it yourself.

Will you need to change the code?
If their code is reliable and does exactly what you need, then result!  If not, then you'll need to change the code.  You might just need to improve the code base to make it more robust, so that it passes a set of tests that you create.  (this is important because otherwise you don't know if the code does the right thing).  You might also need to change the functionality (modifying and/or extending what it does).  In this case, remember that this counts as feature creep, because the original author is unlikely to have predicted the changes you're about to make.

Do you need to learn the underlying techniques?
Scientific code often implements fairly specialist methods.  Whether it's a cutting edge statistical method, a simulation of a complex physical model or a data processing pipeline for highly characteristic data, the implemented techniques might require specialist knowledge to understand.  If this is the case, you'll need to learn at least some of that specialist knowledge simply to understand what the code is doing currently.  Because if you don't understand this, it's not likely that you can make sensible changes to the code.  Lack of understanding is a horrible starting point for writing code.

How much time will you save?
This is the crux of the matter.  Why are you contemplating using someone else's code?  Because you hope it will be quicker and/or require less effort to do so.  So you need to try to estimate how much time it will take to put their code into a form that you can use, then compare this to how long it would take to write the code yourself.  If there's a clear winner, you have your answer.  If the two estimates are similar, there's a chance that you don't gain much by using their code.  At this point, it might be worth considering whether there are other advantages to one way or the other.  For example, you'll have a better understanding of code you write yourself (useful for future re-use).  But on the other hand if their code is written in C++ and you would need to write inMatlab because that's where your skills lie, their code will be faster than anything you could write in a similar amount of time.

And remember to compare like for like.  Don't compare the time to cobble together your own prototype with the time it'll take to adjust someone else's pristine, documented, consummately well-written masterpiece.

In conclusion...
We tend to be cautious about other people's code, but don't discount it out of hand.  Imagine the joy of saving yourself six months of effort because someone else has already written what you need and they've done a bang-up job of it.  Also remember that libraries are other people's code.  They simply have an emphasis on being reliable and reusable.  And we all use libraries.

No comments:

Post a Comment