Monday, June 1, 2009

21,000 Lines of Code

MTG Forge has at least 21,000 lines of code. (I say "at least" because that number doesn't include CardFactory which has a lot of redundant code. CardFactory another 16,000 lines itself which makes for a grand total of 37,875 lines.)

It is hard to picture 21,000 lines of code but if you wrote 100 lines of code a day it would take you at 210 days, so you can easily see why MTG Forge took about a year to get it up and running. This doesn't include specific bug fixes which tend to take up tons of time and only account for a tiny bit of code.

MTG Forge’s source code is roughly the size of Oliver Twist by Charles Dickens. When the source code is compressed the size is 350 kb which is the same Oliver Twist. Oliver Twist is about 400 pages long, although the exact number of pages will vary from book to book, and has around 161,000 words. And for what it is worth, the source code is 1.78 MB uncompressed and Oliver Twist is 914 KB, so MTG Forge’s source code is about twice the Oliver Twist.

Computer programming is like writing a novel or building with toothpicks. It can be done, but it takes a lot of time. MTG Forge is by far the longest program I have ever written.


willow said...

Wagic has around 22'000 lines of codes, including the part that does the equivalent of cardFactory (that I named abilityFactory, by the way).

Forge clearly has a bunch of redundancies that could be eliminated to reduce the amount of code. Wagic too by the way. I realized 20'000 lines is pretty much the limit of what I can maintain by myself, so reducing the amount of code is becoming critical for me, and I guess it is for you too.

nantuko84 said...

I believe not always reducing the amount of code is possible, as an example? code responsible for card implementations really takes lots of lines and you can't do anything with it.
What can be done is separating project into independent modules so it's easies to keep in mind what happens in each of them. Structuring is very important part of programming and I guess should be done at architecture step. Later when you make fixes, they often are made in a hurry or using code-it-so-it-works principle, so they don't fit into your initial architecture, that definitely makes you code style worse and for certain it is hard to keep such code in mind.
Usually in such cases programmers stop working on current version and create version 2.0 from scratch %) and it is true that writing new code brings more fun and takes less time than maintaining of redundant old code ;)

WilLoW :--) said...

nantuko84: Good points, however I have two remarks.

1) the code for the cards in MTGForge could definitely be refactored to take less lines of code and be more flexible. Wagic's code for the cards fits in a huge 5500 lines (it's too much already). With these 5500 lines though, we theoretically handle more than 6000 cards, which is 3 times more than MTGForge, with 3 times less code (so, a ratio of 1/9).
Not trying to have a contest here, but just to say that code could be factored somehow in MTGForge (and rares know it, he complains a lot about that)

2) I don't believe in version 2. Not for MTGForge in particular, but for programs in a general way, especially when they're done by a guy alone. I wouldn't recode my app. Seeing how much version 1 can accomplish, getting there with version 2 would take me more energy than I would be able to put into that project once more. But maybe that's just me. On the other hand, I believe in smooth refactoring...

Forge said...

21,000 lines is long but it is divided up logically. As far as I can see none of the code is redundant. I had no idea how long MTG Forge was until I measured it. 21,000 lines is long for a single programmer but short for a commercial program. (And thanks to Dennis and Rob who have picked up the development and have added great features.)

DennisBergkamp said...

Actually, doesn't CardFactory have 30,000+ lines of code?
MTGForge has about 51,000 lines of code I'd say :)

Belin said...

I haven't really been in the shoes of a programmer so far yet, but I took a quick glance at all the code in cardFactory and tried to understand it a bit, even in the most superficial way... I'd say it was a 90% failure. Programming is *arduous*.

Unknown said...

Dennis and I made some attempts to split Artifact card code into its own mini CardFactory. It sorta worked, sorta didn't. It seemed like the card text wasn't being maintained. The abilities were present, but just not the text. We weren't really sure what went wrong, and we need another pair of eyes, preferably Forge himself.
The idea being that each card type would be stored in its own mini-Factory or a couple mini-Factories for the creatures. This would reduce the size of CardFactory, but the overall code size would stay the same. Each mini-factory would be easier to read and maintain.

Anonymous said...

Hi there,

I just made a little screen cast about my Magic suite. It contains a "workflow" gathering the card images, constructing virtual decks out of these, saving, watching, trading them, ... There is also a booster-draft program, one has to be server and other users can connect, chat, pick boosters and then start the booster-draft. At the moment there are no different draft-types included. Right now they all pick a card round the table in a given order. The next -logical- step will be a program being able to load those decks (constructed / drafted) and allow people to play against each other. This is work in progress.

Before I post you an url where you can watch my little video -if interessted in- I want to make some annotations:

- None of the shown programs are already finished, neither completely bug-fixed nor feature complete
- Excuse my english, I am not a native speaker as you will hear
- Therefore all the GUI Elements are named non-english up to now.
- It's a private project for some friends and myself and I don't know when or wether I will release my Magic suite to public (not at least regarding the law issues)

Memo: All the shown programs sum up to a little less than 6000 lines of code.

I will leave this online a couple of days:

Watch the video:

nantuko84 said...

Anonymous> I like your suite!)
Do you have blog to ask questions? And is it possible to download it?

DennisBergkamp said...

I like the way booster draft is done (with the pictures of the boosters).

Anonymous said...

@ nantuko84:
No, I don't have a blog and there is no public download up to now, only a forum... perhaps I will create a blog some day...

@ DennisBergkamp:
Yes, I figured out that a big part of the feeling when opening boosters is done by the booster itself. My goal was to get as close as possible to the feeling of opening booster packs in a virtual version. Took me some time to get them all together.

Thank you for your positive Feedback! :-)