Categories
Game Development Geek / Technical Personal Development

Finding Ready-Made Code

This past weekend I was trying to work on some code that makes use of math. Specifically, I was working on acceleration and velocity, but I didn’t necessarily need a heavy-duty physics engine. I just wanted some low-level C++ code for vector math.

I shouldn’t have to write it myself, right? It’s been done before, by myself and others, and I’m sure some publicly available code would be better tested and more functional than anything I would write. It should be easier to find some code online than to pull out the math books and write my own. There is no need to reinvent the wheel.

It spent about an hour and a half researching a Vector class. It was difficult to find because a number of hits were for imitators or enhancements of std::vector. When I finally found a good implementation, it was part of a small library, but it was fairly trivial to separate it out. I had to make some changes to adapt it to Doxygen, but it definitely beats creating a complete class definition from scratch over the course of a few days.

It was difficult to find code that was generic enough and well-written. Some were too specific or too unwieldy. I found one library that was a “tiny matrix and vector” library, only to discover that the vector was a small footprint version of std::vector. It was one of those WTF moments. I found one class that would have been fine except that the author didn’t bother to present his name or the date that it was created. How can I keep the author’s name in the source code if he/she didn’t bother put it in there in the first place?

I finally found some useful code through Koders, a source code search engine. I also learned about a few other code search engines that I may use in the future:

Planet Source Code
Krugle

What do you do when you need some basic code that you know should be available as a library? Do you just take the time to write your own, or do you look for code online? Do you outsource it to someone, using a service like Rent A Coder?

16 replies on “Finding Ready-Made Code”

Man, I’ve been there. WAY too many times.

I run into other problems, too. Like:

The Gigantic Do-Everything Thingamabobber
You need a screwdriver, but are instead given this really awesome megalibrary that adds 5 megs to your program’s footprint, and is almost impossible to separate into component pieces (because it’s so much more EFFICIENT this way to have them all share code like this and derive from common classes & stuff). So you have to jump through 50 hoops, change settings, configuration files, and of course add 5 megs to your program just to turn a screw. But hey, if you had need of any of the library’s 5,000 other functions, it’s all there, man!

The This-Mostly-Works-But-Then-I-Got-Bored Widget!
This is really common in Open Source, unfortunately. You find something that gets you about 50% of the way there, but that something is buggy and has missing functionality that isn’t documented as missing. By the time you plow through the whole thing, find what’s missing and what’s broken and what is only mostly-working-with-critical-omissions on something that’s a big general-case solution (not quite as heinous as the above, but still about 4x more than what you needed), you could have written your own. Twice.

The Poorly Categorized Solution
You find exactly what you need going by a different name for some completely different purpose by stumbling across it by accident a year after you needed it. You just could never find it by searching for it.

What usually happens is I find someone’s smaller, more specific solution that won’t work for me directly, but I use it as a springboard to roll my own.

The grand dream of Object Oriented Programming providing us with a gigantic array of easy-to-combine components has never been realized. Though I do occasionally get lucky.

Hah, those are some appropriately named categories, and I think I found examples for all of them in the past few days!

I did find something small and manageable, and I think I might just implement it to get something to work with. I should be able to change it, tweak it, or swap it out entirely without too much effort. Well, hopefully.

Its really odd, I started out just wanting snippets of code so I could *ahem* “write amazingly similar code…” in my project.

But now, most of the time I’m just looking for a general outline of things, even if it something boring like a vector library. I remember writing a lot of boring code a few years ago, but I’ve gotten very good at re-using things. Over time, I think its possible to create your own library of things, which imo is better then grabbing pieces off the net cause you know how your own stuff works.

Perhaps, and I think it is perfectly valid if you are just going to end up redoing it to match your own style anyway, but there are obviously limits. People won’t implement their own low-level libraries because some good ones already exist. Shouldn’t it be possible to extrapolate this concept out to all sorts of things? In fact, I wonder if it is possible to find ready-made libraries for all sorts of needs.

Like RohoMech, I typically have an easy time finding basic code for what I want, and just have to add a few things I need, like a binary heap class that I needed recently. I found a nice little binary heap class, but it didn’t have the ability to re-sort the heap from a given node. So I just added that one piece of functionality, and I was happy.

So… I guess I should really be releasing my improvement of the original binary heap class, shouldn’t I? Maybe too many of us are keeping our code to ourselves? tsk tsk, shame on me.

Heh, perhaps. I think most people have trouble finding good code, so they always write their own, but then only a few of those people will release them.

Would it be better if people released their code more often? Would it just be more noise, or should people stop worrying that the code is too ugly or immature to be released?

Fear of criticism might be one reason I keep my code to myself. The huge on-line community of computer geeks can be very 1337 (not always). If I can solve a problem to “get the job done” and perform well for my applications, then I will leave it there.

If I’m going to release some code, I would want to document it much better, show some examples of how to use the code, and make it as clean and readable as possible. “You should always code that way!” Well… I don’t have that kind of time. 🙂

This is kind of similar to something I was thinking about the other day. I’ve been doing a lot of miscellaneous programming as part of my classes, and a lot of it is stuff that I’m sure almost everyone has had to write at one point or another. I was thinking about doing a Code Snippet post every couple of days or so. Just a class that does this or a method that does that. Nothing too fancy but perhaps useful to somebody.

Neil: I agree that we don’t always have the time to clean things up for mass consumption, but if it works well for you, couldn’t someone else find something useful? Maybe someone with more time can work on it and polish it up?

Casey: Code Snippet posts might be pretty cool. At the very least, you’ll get some feedback on possible improvements or even references to how others tried to handle the problem. In fact, I may want to post some code snippets myself.

GB, I think the larger issue is frameworks vs APIs.

By frameworks I mean, its some body of code, you overload / insert you code into some parts, and the framework chugs along and calls your code at the right moments.

By API’s I mean, you have an application, and you want to perform some function, so you link to a library / DLL and call that function. You might need to do a bunch of initilization, and it might be hard to get things work just right if you don’t.

Things in the API category are usually what I’ll end up re-doing myself. Mostly because its not much work, and because I want test cases for everything, which API’s won’t let you do. But, I think this is exactly what you want code for, that single API function being called.

Frameworks on the otherhand, like if I found a Shooter Framework, I could just change a few things (like enemy types, bullet types etc) and it’ll become the game I want. Things like that I’m not going to bother rewriting.

I’m still waiting for Google’s sparsehash implementation to be finished… :(… ah well… it isn’t like I need it as this very moment.

In general I don’t tend to go digging for code bits since most of what I need to do is covered by the “engine” I’m using, T2D. That said, there’s a few things that I just go get a library to help me save time and extremely low-level programming.

Things like MIDI playback, physics, etc. are definitely on the list of “don’t code from scratch unless it’s just intended as a mental workout / pain-tolerance testing method.

-Tim

I would like to release my Lua script C++ wrapper. I think it is extremely useful, if I do say so myself. But… where do I release it? Sourceforge? Hmm…

I imagine that eventually I will have my own code archives that I will trust more than anything I find online, but my archive is still starting out. Besides, finding code teaches me by showing me what elegant code can look like as well as case studies in How Not To Do XYZ. B-)

Comments are closed.