Monday, May 24, 2010

Coding with Java and Forge

(This article is only about programming but I don't want to discourage non-programmers from reading it too.)

Transmuting an idea into words can be difficult. (Hey, you have to give me extra credit for using a Magic keyword.) The concept is that Forge's cards are technically in Java but cards must also use the Forge framework. If you only know Java, you won't understand how Forge encodes cards. You have to understand how Forge does things.

Let me throw a quote at you from my favorite book Code Complete. This is talking about high-level code that is written in terms of the problem domain, "Code at this level won't depend much on the specific features of your programming language because you'll have built your own set of tools to work with the problem. Consequently, at this level your code depends more on the tools you've built than on the capabilities of the language you're using."

The idea of using an existing framework usually appears in bigger projects but JUnit is a good example of a smaller program. A framework tries to be "easy for someone else to learn" even though it takes time to really understand the framework. There is a ton of interesting Java libraries but many of them have a steep learning curve. I haven't touched the physics library JBox2d because it seems so complicated. (I want to use JBox2d to program a free version of Fantastic Contraption. I don’t want to ripoff their artwork but maybe I could copy their style?) fantasticcontraption.com

OK, back to Magic. When I was writing Forge I didn't realize that I was writing a framework that combined Java and Magic. Somewhere after I wrote a few cards it hit me that Forge's framework isn't very flexible and there are some cards that it would never support. This is always a scary time because part of you wants to start over and do it better but I forged ahead anyways. ("Forged ahead", I'm a really funny guy, I think.)

The tricky thing when writing a framework is that you don't really know all of its limitations. One day you may think, "Forge will never support Glorious Anthem" but then later you (or someone else) comes up with a nifty little hack and voila, you're using Glorious Anthem in your white weenie deck. You have to use a framework for awhile in order to really understand its limits.

Forge's framework is pretty good. There are obviously some rough spots but overall it works "good enough."

p.s.
Code Complete goes on to say that your high level "code should be somewhat readable by someone who's not a computer-science whiz". Forge tries but doesn't quite get a gold star. Although at least 2 people have learned Java just so they could work on Forge, so at least I did a decent job on the framework. Frequently frameworks come with external documentation so you don't have to understand the details of the code in order to use it. This is the Holy Grail of programming, code reuse.

Writing code that someone can understand is hard. Writing a framework (much longer code) that someone else can understand is much harder.

No comments: