Thursday, January 14, 2010

Functions are not the devil

Hypothetical: your initialization function is a huge mess. It's probably 200 lines all by itself.

What do you do? Break it up into functions. Functions are not the devil.

That's right, you can make all of your messy initialization functions into clean little bits of comprehensible code with this amazing programming feature called Functions!!!

Turn your initialization function into something like this:

function Init()
{
g.log.Add (1, "Initializing level "+g.level+"...");

// making the level
level = lm.LevelFor(g.level);
PopulateLevel (g);

// the player
InitPlayer (g);

// display initialization
InitRadar (g);
InitDisplay (g);

// stamping the level stats
StampStats (g);
}
With the magic of the almighty Function!!!

In all seriousness, the programming world has functions for a reason. I'm not trying to criticize the lack of functions in code. I'm simply trying to help people realize that having a thousand short little functions is a lot better than having twenty monolithic functions.

But you had better have some sort of function naming scheme in mind or things can get very hairy very fast.

No comments:

Post a Comment