As a small team making a massively multiplayer game we need all the help we can get! When choosing our back-end technology we knew we didn’t want to create it from scratch, but take advantage of a company that would provide a Backend as a Service. They would manage all the infrastructure and provisioning, leaving us to concentrate on writing the game logic.
I’d used a few of these services before so had a pretty good idea of what I wanted. After reviewing all the options available, GameSparks came up as the best of the bunch. It was super easy to get up and running but then it came down to actually writing the code. With only two programmers on the team, and George being a bit of a graphics whizz who is better placed working in Unity, that task fell to me.
Stuff I Hated
Scoping of Variables
As any programmer knows, if you have a question about how to do something in a language, an internet search usually takes you down a path leading to Stack Overflow. Having gone this route many times, it drives me slightly mad that the best accepted answers are usually how to do the thing in jQuery. GRRRRR - I’M NOT USING JQUERY!!!!!
This is getting increasingly ranty so I should probably stop and move on to...
Stuff I Love
Revealing Module Pattern
In the fight against chaos, programmers like their code to be modular. I can’t find the original link where I came across the Revealing Module pattern, but its discovery came as a great relief as here was a sensible way in which I could structure my code. I could now control which functions were public and globally available and which ones were for private use only, as well as grouping them by functionality - phew!
Array filter() and map()
Array.filter() returns a new array containing all of the elements from the original array that satisfy the criteria defined by the function passed in. Take a look at the code snippet below where I’m searching through the cards in the player’s hand to find playable weapons. It is easy to see at a glance that the second method is simpler and more elegant than the first; there is less code, and no need to declare the variable i, plus the explicit use of filter makes it easy to establish the motivation of the programmer.
Similarly you can see below an example of Array.map() which returns a new array containing a value for each of the items in the original array, therefore mapping one type of object to another. Again, the use of the function makes it much easier to establish at first glance the intention of the person who wrote the code.
With all of the built in array functions, eliminating the need for a loop gives the programmer less opportunity to introduce bugs. When writing a loop it is easy to make mistakes, such as forgetting the var keyword, using > rather than < or + instead of ++, and yes, I speak from experience!
Functions as First Class Objects
Stuff I'm Still Working Out