Design Patterns – Intro

Image result for persona 5

As usual, I couldn’t find a decent picture, so I just put up one of Persona 5. I am still playing it, it is really good. Pick it up!

Recently I have been going through all my knowledge and figuring out where my core knowledge gaps are. After doing a few tests, etc, I found one of these was design patterns. I have used them a lot in the past, but there are still a few that I am not super comfortable with, so as with most things, I thought I would revise them by doing blog posts about them. Makes sense right?

Firstly, why are design patterns important?

I have worked with a number of code bases, both good and bad. Some code is built in an extremely nice way, other bits are hacked together willy nilly just writing code and not really thinking about the architecture to solve the problem. I myself have done both at some point or another, for a variety of reasons that I may go into in a later post. Design patterns CAN help with this, but there are some caveats.

Let me go on a bit of a tangent a bit. In 2017, games are often not a fire and forget bit of software. They are living breathing things which we can later update and make better. We have platforms like Steam, PS4, Xbox One and Switch which allow us to patch our games unlike the days of PS1 and PS2. We also have games that require lots of Live Operations and will require us to revisit code to add features or extend existing ones. We will essentially need to make changes and design patterns are one way that can kind of help us write code in anticipation that we are one day going to have to make these changes. They are no silver bullet, but they can help towards this.

But hang on, why is that the case? Well let’s look at the definition or what a design pattern is.

According to Wikipedia – “a software design pattern is a general reusable solution to a commonly occurring problem within a give context in software design.”

Let’s say you get to a code base  and you need to make a change to a feature that you are not super familiar with. This feature is meaty, but luckily as you peruse through it you can see some familiar patterns and the classes, etc seem to be named in familiar ways. You realize that someone has used a design pattern and you can use your knowledge of the design pattern to get stuck in. The amount of time taken in understanding the code has decreased and the amount of time needed to make your change. This is because they are defined as reusable solutions to commonly occurring problems. Yes you will still need to learn what the code is actually doing, but as it is a solution to a problem that has cropped up before, their usage is communicating to you what the goal of this system was and it’s intention. Similarly, as it is a familiar solution to a commonly solved problem, the code is likely to be more readable and thus also easier to maintain, which is good as someone else comes back and revisits it again.

You have been given a feature on the game to do. It is pretty big and you need to architect a system. You come up with a system that fits within a design pattern. By doing this, you can easily explain to other developers how exactly you are going to implement a feature. You can talk about how you are going to use a Factory to handle all your object creation, or an Observer to handle when to trigger an event. It gives developers a common vocabulary to talk about and can be used as a good starting point in a discussion whether or not that it is a good way of implementing the solution to the problem you are trying to solve.

Here is a final example. Say you are writing a feature, and it sounds very similar to something that already exists. You look up the code for this old feature and find the underlying system is written in a nice reusable manner as it followed a specific design pattern. This is awesome as it means you won;t have to write a tonne of common code to solve your issue.

So let’s look at the key points at why Design Patterns are important:

  • They make code more readable
  • They make code easier to maintain
  • They can make it easier to communicate your intentions to other developers
  • They allow for code re-use and less code

Sounds great, right. There are a fair few good reasons why you should use a design pattern, but there are caveats. Like I mentioned briefly before, they are not the silver bullet. And can cause problems.

  • They can make the code more complex – some of them patterns use a lot of indirection and less experienced members of your team may have a harder time following them
  • You need to know when to use them – There are always dangers of using the wrong pattern and also using them just for the sake of it. Regardless of how senior you are, when developing a big feature and want to use a design pattern, chat it through with another developer. Otherwise it will make life harder rather than easier
  • Some can hurt game performance – patterns can make your code more flexible and easier to reuse, but this can in some cases come with a cost to performance. They can rely on features that have a greater runtime cost. There needs to be a balance here. Although you are writing more flexible and potentially easier to understand code,a re you shooting yourself in the foot when it comes to performance?
  • They can be abused – Google the Singleton pattern and you will find a lot of discussion around its abuse.
  • You can code yourself into a corner – This is a very specific case I have found when it comes to dealing with live games. Originally, you used a certain design pattern to create a feature based on the design. However the game is out in the wild and analytics are revealing that players are having a hard time with it, don’t enjoy it or it just doesn’t work as a concept. The game is a living breathing thing and you have to adapt it. The change you have been given is not that big conceptually, so you go to the code base. However, because you built the code in such a specific way, it is actually going to take a long time to unpick parts of it and put in this change. You essentially coded yourself into a corner.

So yeah, Pros and Cons. The main points here are:

  • Design patterns are powerful and can help make your code better, when thinking about using them, research them and discuss your solution with others to evaluate the pros and cons.
  • Don’t be the “Small Boy with a design pattern” and use them for the sake of it

Awesome! Next we will be looking at some of the Command Pattern!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Verify that you are not a robot! (If you are unlucky) *