So I’ve started my final project, I’m doing a mentor project working on a strategy game with a working name of “Trading Company”. When I joined this project, there was already some code written by another student developer, he wrote a simple framework for me. There were classes for a Trading Company, a Bank, an Employee, etc.
The first week or so, I was doing great, adding tons of new features, getting the work done at a good pace. The problem came when some changes needed to be made, we wanted to change the mechanics of how a job in the game worked.
Originally the jobs were setup to just send workers to a location, have them do some work, and return with resources. However we wanted to change the system so that each location had it’s own inventory for items and employees stored at that location. So now you would send employees to a location, and they would stay there, and from that location you could send them each on missions.
Well originally this wasn’t setup in a way to easily accommodate this change, the code was a bit messy. Items were being represented in two different classes (Resources vs Products) and it led to issues trying to add or change these types. Employees were also being represented in arrays with sizes of four (the amount of employee types). So anytime I wanted to go through and make changes to the employees I had to loop through at least one for loop.
In general, the problem was that I hadn’t set up the system to be very object-oriented, which was a major mistake. It took me a couple days to convert the system to be object-oriented. Now instead of arrays of specific types of employees, I had an EmployeeStack class, and this just stored a large list of whatever employees you wanted, with functions to get counts of certain types of employees. Similarly I made an Item class (which combined the Resource and Product classes) and then an ItemStack class which contained a list of Items.
From there I had a Location class that stored information any location would have, an ItemStack and an EmployeeStack. So now all the different locations had their own individual information for what that location stored.
Now that the code is much cleaner, it’s object-oriented, adding new features is once again a breeze. Changing any minor or major features is no longer a daunting task, because the code is setup to be changed however I want.
Including some WIP screenshots of the game below!
0 comments on “Trading Company – The Importance of Clean Code”Add yours →