Getting Attached to Code: Don't

Hey.

It’s been a while. I haven’t kept the promise of posting more often, but I’ve been busy with my latest project.

I started on San Andreas Battle Royale without a proper plan in place (the first mistake). My first commit to the project was an authentication system that I intended to scale into a website (think of a user dashboard, where players could change their attire and view their statistics) using SQLite (the second mistake). This authentication system was already too spaghetty for my liking, but I was motivated to continue.

Shortly after, I started working on the matchmaking system inspired by another member of the San Andreas Multiplayer community. Below is a preview of the matchmaking system:


// 10 players needed to start a game.

#define MIN_PLAYERS 10

enum E_GAME_STATE {
    GAME_STATE_WAITING,
    GAME_STATE_COUNTDOWN,
    GAME_STATE_ONGOING,
    GAME_STATE_FINISHED,
}

new Iterator:gamePlayers[MAX_PLAYERS];
new gameState,
    countdown = 90;

// Whenever the server is started, we set the game state to GAME_STATE_WAITING.
public OnGameModeInit() {
    gameState = GAME_STATE_WAITING;
}

// Whenever a user clicks the "Play" button seen below, his ID is added to the gamePlayers iterator. I'll exemplify this with a command.

cmd:play(playerid, params) {
    if(gameState == GAME_STATE_WAITING || gameState == GAME_STATE_COUNTDOWN) {
        if(!Iter_Contains(gamePlayers, playerid)) {
            Iter_Add(gamePlayers, playerid);
        } else {
            Iter_Remove(gamePlayers, playerid);
        }
    }
}

// To check if the minimum amount of players was met, I had this function running in a 1000ms timer, repeatedly.

OnGameModeUpdate() {
    switch(gameState) {
        case GAME_STATE_WAITING: {
            if(Iter_Count(gamePlayers) >= MIN_PLAYERS) {
                gameState = GAME_STATE_COUNTDOWN;
            }
        }

        case GAME_STATE_STARTED_COUNTDOWN: {

            // Decreasing the countdown (default value: 90seconds) by one each second.
            countdown--;

            if(Iter_Count(gamePlayers) < MIN_PLAYERS) {
                gameState = GAME_STATE_WAITING;
                countdown = 90;
            }

            if(countdown == 0) {
                gameState = GAME_STATE_ONGOING;
            }
        }

        case GAME_STATE_ONGOING: {

            // Clean the user's screen.
            // Put the user in a plane.
            // ...

            if(Iter_Count(gamePlayers, 1)) {

                // Finish the game as there's only one player left.
            }
        }
    }

    return 1;
}

// Working with player's deaths.
public OnPlayerDeath(playerid, params) {
    if(gameState == GAME_STATE_ONGOING) {

        // Remove the player from the iterator which contains players that are in a game.        
        Iter_Remove(gamePlayers, playerid);
    }
}

If you’d like to see a preview of what I had up to this point, click the image below.

Preview Preview of San Andreas Battle Royale authentication and matchmaking.

I wasn’t too unhappy with the way the way things were working, but it was when I started to branch out of solo mode that I realized that I couldn’t continue working with such a messy code. At this point, I had probably put over 14 hours of work into this project and I was quite happy about picking up Pawn again and doing something cool with it — but that wasn’t enough to stave me from deleting the entire project and wanting to start again.

Thing is: I haven’t started again yet. It has been four or five days since I trashed the entire project and I started to write everything I wanted to do with this project on a text file: ideas.txt — how convenient. Despite that, I still have this feeling of attachment to the code that I spent hours working on, but I don’t regret a tiny bit deleting everything. It is part of the process. Building software is a craft, one which you only acquire through years of practice. Despite being part of the process of learning how to build software, you should never hesitate to do a full rewrite of your product/project if it’s in an early stage. If you’re in an advanced stage of the project (specially if it’s commercial software and close to being finished) and dislike the way you’ve crafted the software: ship it and then start gradually fixing the codebase.

I haven’t started work on this project again because it has been distracting me from this blog and I seriously want to do some cool stuff with this blog, therefore I’ll probably put it in the idea box only to get back to it later on this year. For now, I want really really want to start writing more cool stuff to this blog. I have a few cool ideas written down, but I’m not going to promise that you’ll hear from me very very soon again: but I sure hope you do. I’ll do my best to make that a reality!

Thanks for reading this. This almost feels like me talking more about my project and what to do if you get attached to code than why you shouldn’t get attached to your code, but the essence of this whole post is that you shouldn’t get attached to your codebase just because you have spent a lot of time on it. Write code with a plan and you probably won’t run into this issue.

Happy coding.