Apr
27
Laziness (ie, using someone else’s code)
Filed Under Everything |
It has been said that laziness, to a point, is a virtue for programmers. Actually, most people leave out the “to a point” part, but I’m guessing it’s implied. As I’ve mentioned before, I take this philosophy pretty seriously. If I can avoid writing code well, by golly, I’ll avoid writing it. So, today I’m going to avoid writing code, and I’m going to do so by using code someone else has written. I think many programmers actually have difficulty with this idea. I know that there was a time in my life that I did. Every now and again, I still do. I had this silly name for using someone else’s code: “black box programming.” And I hated it. So I wrote every last little routine, every single little bit of code a program required, regardless of whether there were implementations already freely available. The result, I hoped, would be that I would learn more about programming by doing this. Now, I’m not saying it wasn’t educational, but, the real result was that I never got anything done. Ever.
Not to say there aren’t reasons to not want to use someone else’s code. There’s a lot of issues to consider. It can be difficult to assess the actual quality of the code. And, while doing so, you have to be careful not to pick nits (”This is supposed to be C++ code, but all of the files have a .C extension! This code must be horrible!”). There’s also documentation. Some very nice code comes with no documentation, some horrible code comes with extensive documentation. Licensing. If you’re planning to ever sell or give away your software, you’d better be sure the code you use has a license you can deal with. And, of course, finding the code in the first place can seem to be almost as much of a chore as just writing it yourself.
Almost, but often not quite. The benefits you can reap if you find a nice, well written, well documented bit of code that fits your needs can easily outweigh all of those issues, except perhaps if it has a nasty license. And, sometimes, even that can be dealt with.
This post started yesterday, when I discovered a subtle, but fatal bug in some vector math code I had written (for this blog, in fact). It was dumb, just a typo, but it took longer then I would have liked to track down. After that, I decided it would probably be a good idea to put together a test suite for the code, and find any other issues that might be lurking before I move on.
But, see, I’m lazy. And it was almost time to go to work, anyway.
I realized that there were probably dozens - nay, hundreds - of open source vector math libraries out there. If I were lucky, I’d be able to find a really good one, and heck, maybe it’d even have support for matrix math, too. Which I’d also be needing in the future.
So I went a-searchin’. I started with trusty old Google, but decided I ought to narrow the search a bit and took a peak at Sourceforge, where I found (the creatively named) Template Matrix/Vector Class Library for C++
by Mike Jarvis. TMV for short.
This library is definitely overkill for the little projects I’ll be using it for. But, it does everything I need, uses a coding style I enjoy, includes a very thorough test suite, and is well documented. The only thing that might bother me about it is that it’s licensed under the GPL 2, but since I’m just using it for little open sourced demo programs anyway, I’d say that’s fine.
Now I have nice vector math functionality, and complete matrix math support for when I’ll need it in the future. And thanks to the test suite, I can be confident in the results I get out of it. I barely lifted a finger.
This is just one example. In a future episode, I will be introducing you to yet another piece of someone else’s code that I’m using.
Anyway, at this point I’m rambling. The point of all of this is that there are lots of problems that need solving when developing software. Many of them have been solved over, and over, and over again. Do you really need to be spending your time solving them again? Well, maybe if your boss tells you to. But if you have the ability to be lazy about it, and find an already existing solution to use then, well, why wouldn’t you? I heartily recommend laziness.



