January 26th, 2017
I saw a tweet earlier today "there seems to be this obsession with turning everything into an object". Here's a detailed response as to why using objects instead of arrays is a good idea, in my opinion.
I once took over a project written in ActionScript 3. It was a fairly simple project. Five mini-games and a progression scenario to tie everything together. The developer who left was obsessed with arrays. You want to place an interactive object with its associated graphics, sound effects and behavior? Make an array. You want to configure a cutscene with character movement, dialogue and effect on environment? Make an array.
Here's the problem now. There was no way for me to know how to properly set up the array. I didn't know which array keys were available to me. I also didn't know what to put as the value. Is the delay in seconds or milliseconds? Is the speed expressed as 50 or 0.5 if I want half speed? How do I tell the sound clip which character's mouth to move? Do I refer to the character by reference, by ID or by its position in the characters' array? There was no documentation and no validation. The only way I had to know is to read the entire code and reverse engineer the docs. It took the guy 2 months to write the code and it took me 1 month to understand how the heck it worked.
This is one problem. There are more, but I'll refrain from writing a whole book tonight.
Enter objects. Yes, objects are more verbose, especially when you need to instantiate many of them to configure a complex graph. You also need to create those pesky classes. But that inconvenience pales in comparison to the power that they bring.
Let's configure a theoretical cutscene. I'll use simplified syntax to help non-AS3 people understand:
var cutscene = new Cutscene(); cutscene.addActor(new Actor('king', 'path/to/graphic')); cutscene.addSpeech(new SoundClip('path/to/clip1'), 'king'); var effect = new Effect(); effect.setReputation('castle', -200); var response = new Response('This is nonsense', effect); cutscene.prompt([response]);
Take a moment to thoroughly read it. You can probably see the cutscene in your head and you know exactly what happens when the player picks the response. It's easy to read. Also, you know that there are more methods in the cutscene to explore, and more effects to apply. Open up the classes and voilà! you can probably figure stuff out without docs.
An added advantage is that these methods can now validate your arguments. Adding a speech to an actor who's not there? Throw an exception. Setting a reputation on a non-existing faction? Exception. This way you'll know how to fix it, instead of hoping for a meaningful error during a runtime crash.
I've seen codebases on which one developer works at any given time, but it's a different one every few months. Other codebases are used by teams of 30. And then there are open source tools that can potentially make life miserable for thousands. Even you're the only one who will ever touch it, you will still forget the proper array structure a week from now. This is especially true when you temporarily switch to a different project for a day or even a week.
What are your thoughts on this? Anything to add? I'll see you in the comments below.