Sunday, November 06, 2005

How not to code:

Trawling through my code base seems to be like breaking open a rotten orange. I found this gem of an interface:

namespace EINFALL.Game.World
/// Summary description for ILocalMap.

public interface ILocalMap : IAbsoluteBounds
void RefreshTilePositions(); //If map DX pos changes
//Check if one MapObject's potential boundary collides with another map
bool ObstructedByMapObject(BlockBounds boundary, MapObject focus);
bool ObstructedByMapObject(PointF point, MapObject focus);

bool IsPointBlocked(PointF p);
bool IsPointBlocked(float x, float y);
int GetTileIndex(Tile t);
int GetTileIndex(float posX, float posY);
int GetTileIndex(int x, int y);

void RemoveMapObject(MapObject m);
void AddMapObject(MapObject m);
//Remove a map object after it finished being used
void KillMapObject(MapObject mo);

//Is some map object potetially going to break the map boundary?
void TransferCheck(IAbsoluteBounds aBounds, MapObject mo, Breaks breaks);

void RenderDetail(float x, float y, Device device);
void RenderBase(float x, float y, Device device);
void RenderSubSection(Point startPoint, Point endPoint,
Device device);
void RenderSubSection(Tile startTile, Tile endTile,
Device device);
void Render(float x, float y, Device device);
Tile GetTile(int x, int y);

void SetMetaMap(BufferedMap m);

//Could probably be turned into getters
Tile[] GetTileArray();
void SetTopLeftDXPosition(float x, float y);
void SetTopLeftDXPosition(PointF Position);
Point GetShellPosition();
int GetTileDimension();


It seems to have nearly every function as the map class but with near everyone of it's function abstract.

It did make sense, in the beginning, to have a ILocalMap class. . . well at least I think it did. Well I made sense to have an abstract Map class - I was definetly going to have more than one type of map class - that would probably share functionality.

Some how that reasonable intention turned into the above monstrosity - nearly all the functions are only relevant to one type of map! This morning I've been cutting away dead code - that randomly threw up a few errors - miles away in the code! Suddenly there seems to be a endless loop happening now and again in my map functions. I think I've squashed it. I know I've at least fixed one oversight but who knows what else may be lurking.

Anyway I'm slowing hacking away at the code and we'll see how it goes. I'd love some good recommendations for OOD, OOP books actually. Maybe I should grab the a patterns book too?

* Turns out I needed this interface beast so I could have a skeleton map too. One that did nothing but existed while a real map was being loaded from the disk. I'm almost certain that this is the most messy solution I could have come up but it does work.


Nick Lecrenski said...

A great book is head first design patterns, it's relatively new and based on java but it really works across all languages, it makes design patterns actually make sense. Anyway just wanted to let you know i read your blog weekly, keep up the good work

balaam said...

I did a few searches on the book, it's sounds useful I may grab it! :D Thanks for the tip.