Wednesday, January 13, 2010

5 Ideas That Will Improve Your Code

I've been programming more and playing Magic less, so some of my posts (like this one) are only about programming. Hopefully I'm not alienating my audience. I'm not sure who exactly reads this blog but I'm guessing it is a mix of non-programmers, programmers, computer literate Magic players, as well as a few people who program and play Magic. (Hi to frwololo, nantuko84, DennisBergkamp, Rob Cashwalker , etc...)

These points are taken from the book "Code Complete" by Steve McConnell.

1. The less you know about the internal workings of another method, the fewer assumptions you make about how the method operates. The fewer the assumptions you make, there is less chance that one of them is wrong.

2. The goal for individual methods is to make them like a "black "box". You know what goes in and you know what comes out but you don't know what happens inside.

3. The more independent the subsystems are, the more you reduce the complexity. Carefully defined modules separate concerns so you can focus on one thing at one time.

4. If you have data leaks between the subsystems because of sloppily defined interfaces, you will have a flood of unnecessary complexity.

5. A blanket attempt to avoid mistakes is the biggest mistake of all. Design is a process of carefully planning small mistakes in order to avoid making big ones.

The first and second points are basic but still very important. Sometimes I forget that objects and methods shouldn't know the internal workings of each other. Writing methods like "black boxes" will improve your code.

I think that the third point is the most important. Forge has several parts: IO, user interface (gui), decks, rules implementation but I didn't design these parts to be separate which results in the fourth point.

The fifth point made me laugh because I thought that the design process tried to make everything perfect. The program should be perfectly planned and each part of the program should be perfectly defined and everything is just perfect. The truth is that program design is a very sloppy process. Mistakes are OK but you try to make small mistakes instead of big ones.

Early mistakes (on paper) are less frustrating than later mistakes which are etched in code. Early mistakes are MUCH easier to fix.

Internally Forge has two play areas. I misunderstood the rules and thought that each player had his own "in play" area (now called battlefield, yeah). The problem occurs when you steal a creature that has a "come into play" ability and Forge thinks that creature is being played and the ability triggers. This problem can be fixed by some clever coding but it all stems from my initial misunderstanding. Changing the code to create a unified "in play" area would be a nightmare. (Mostly the user interface (gui) code would have to be changed.)


wololo said...

I made the same "2 play areas" mistake in Wagic, but in the end it's not so bad. An exception needs to be coded for cards that go from one player's zone to the opponent's zone, but it allows to consider the "inplay" zone like any other one (graveyard, library, hand...) and have generic code for zones. It's also very helpful to get the controller of a card at a given time.

Thanks for the hints, they "sound" obvious but they're actually not, it's good to get reminded of good coding practice once in a while

CatzPaw164 said...

Love you entries on coding techniques. I still reference my first edition of "Code Complete" it is full of sticky notes, tabs, and highlights. Keep up the good work!

Anonymous said...

Hi, im new to this site. This site's fantastic, ive learned a lot from here today!

Forge said...

Code Complete is MUCH better than all of my other college books combined.

Forge said...


"Thanks for the hints, they "sound" obvious but they're actually not, it's good to get reminded of good coding practice once in a while"

Well everything is essentially the "fundamentals". If you forget them, you don't have much to stand on. ;-)

Bobblegate said...

I've been reading your posts for a while. I plan on writing a similar Magic program myself one day for the BlackBerry, so everything you've said has been extremely useful. Thank you for your valuable insight!

Anonymous said...

Nice dispatch and this enter helped me alot in my college assignement. Thank you as your information.

nantuko84 said...

My oven has a small lcd with a lot of buttons below. I'm planning to write Magic program for it, so I can play mtg and fight versus AI anytime I cook. just kidding ;)