I started my latest project which is a simple game with a high difficulty level (think Flappy Bird) where gameplay can be extended by answering increasingly difficult questions. It might be chocolate covered broccoli, but I’m here to learn.
I’m writing the game in Swift (as a learning exercise as well) and ran into something interesting. I had run across the common wisdom to default to using Structs over Classes. I followed this advice without completely understanding it. I knew that Structs were passed by value but with their ability to include functions they were mostly like Classes.
Until They Aren’t Alike
I was happily using Structs everywhere until I needed an Array as a property. I added and initialized the Array property just fine, but when I tried to assign a value to the Array I got the cryptic message: “Cannot assign to the result of this expression.” Looked like a valid Array assignment to me. I went and created a Playground to test the Array syntax and there was nothing wrong with that. So I did some Googling and eventually ran across this note about Structs: “The structure’s primary purpose is to encapsulate a few relatively simple data values.” Something in my mind told me that Arrays weren’t simple data values. I changed the declaration from Struct to Class and the Array assignment worked as I expected.
This is the pragmatic way I learn. I pickup generally accepted patterns and practices for the tools and languages I’m using and use them without complete understanding. Some would see this as problematic because I’m moving forward without complete knowledge of what I’m working with. I argue that complete knowledge of the tools, while desirable, is impractical. Using the commonly accepted wisdom to solve known problems frees me to add value by working on the solution or application I’m writing. I also have the confidence that when I run into problems while using the tools I can figure out and increase my understanding of them as I need to do so.