Sponsored By

Code Quality in Games

The quality of a game is often defined by the quality of the code that’s behind it; high quality code is easy to read, comprehend and expand. Expect lots of short code samples readable for all levels!

Joris van Leeuwen, Blogger

June 19, 2016

8 Min Read

What is Code Quality?

The quality of a game is often defined by the quality of the code that’s behind it; high quality code is easy to read, comprehend and expand. It defines how easy we can turn creative ideas into functional results.

Code Quality - The quality of the code itself

That’s right, it means we’re not talking about the stability of the final product. It is the actual code that has the quality or not. Code quality can only be measured by opening the actual code.

Well then you might wonder: WHY? If Code Quality is not noticeable by our end-users, why would we even bother? Well, I’ve come up with three reasons.

1. The love - Programmers love to puzzle and hate to decipher.

We like to have all of the pieces of the puzzle in our heads and move it around to find a perfect fit. We don’t like having to work to get those puzzle pieces into our heads in the first place.

2. The investment - Code is read more often than it is written.

Naturally, new code is written only once. The amount of times you or your teammate is going to read the code to add, edit or remove something is far greater. It is therefore a good investment to focus on code quality.

3. The independency - Code that tells a story.

Sure.. initially, code is just telling a computer what to do. But quality code also serves as a medium to transfer knowledge. Quality code is like a good story to the reader. It doesn’t need the writer to explain what is happening.

Quality Code is Readable

Code that has high quality means that it is readable. Let’s take a look at the following example.

If you look at this code in the right way, this code is kind of like a small story.. Maybe even a base for a novel! It’s about a parent who gives birth to a child and wants to take care of the child. It makes sure to listens to it for when it’s hungry. At one point the child got hungry! So the parent decided to feed the child by creating a piece of food and letting the child eat it.

Ok.. maybe it won’t be the next blockbuster in cinema, but it’s a small story nevertheless! This code is readable because the class, field and method names make sense. They make sure the reader understand what is going on with a minimum amount of effort needed.

Quality Code is Independent

Code that has high quality means that it is independent. Take a look at the following example.

The example defines a child that gets hungry when its energy drops below a desired amount. It’ll need to eat to get get it’s energy back on track.

This child is independent of whatever feeds it. It could be fed by a caring parent, like it did in the example about readability. But it could also be fed by a very cruel parent, that only feeds it when it is NOT hungry! Or maybe something that is not its parent at all.

The point is that, when reading this class, we do not need the knowledge of other classes to understand how this one functions. It is independent; the complexity is limited to the class itself.

Quality Code is Comprehensible

Code that has high quality means that it is comprehensible. Take a look at the following example.

A simple interface for a television set. It has a channel and a volume, and you can turn it on/off, change its channel and its volume. Notice how I defined the interface as “simple”.

I always envision a class or an interface as a television remote; when there are too many buttons I simply cannot comprehend all of its functionality. This is true for classes and methods as well.

Classes and methods that get too big are hard to wrap your head around. Adding functionality or localizing a bug is difficult because there is so much going on! It’s usually because the class or method has too many different responsibilities. To write quality code you’ll need to divide them into smaller ones, so you can isolate their complexity.

Quality Code is Tight

Code that has high quality means that it is tight; we keep everything as locked as possible to reduce complexity. Take a look at the following example.

Look at that, we already start off with making our code tight. There is no functional reason to make the Person class “abstract”.. but it makes sure the Person class itself will never be instantiated.

In the second line of code we use access modifiers to make the SkinTone field publicly gettable, but only this class and derived classes can set it to a new value. Again, there is no functional reason for us to do this - the game would run either way. But we reduce the amount of possibilities and thereby reduce complexity.

The UpdateSkinTone() method is marked as abstract, which forces all derived classes to include an implementation. This, again, is an example of making our code tight.

We force the code to only allow us to do what we want. Private, constant, abstract, readonly, sealed.. they are all hints that allows us to focus our brain power on what matters: making great games!

Quality Code is Scalable

Code that has high quality means that it is scalable; it’s expandable with minimal increase of complexity. Take a look at the following example.

A level with a list of enemies. When updating the level, it updates all containing enemies. This code is scalable because the level is only referencing the enemies by their abstract Enemy type.

It means we can add more types of enemies by simply adding more and more Enemy subclasses, without having to update the Level class. We can expand our feature, without increasing depths of complexity.

Scalability doesn’t stop at isolating complexity - it can also result in efficiency. Let’s take No Man’s Sky for example: everything they made is a result of focusing on scalability. In such a way that they can go up to an incredible amount of content, just by sliding a button.

Quality Code is Soft

Code that has high quality means that it is soft; it’s easy to tweak because values are set outside of any algorithmic code. Let’s do a quick scan over the following example.

So we have an inventory that we can add items to. It can contain a limited amount of items and its items are rendered in a grid layout using the relatively complex algorithm in the Render() method. This code is soft because all tweakable values are set outside of the algorithmic code.

We can easily tweak the amount of columns that are rendered by simply changing the COLUMN_AMOUNT constant in the top of the class. Likewise we can increase the item capacity by changing the associated constant without having to dive into the complex algorithms.

Soft code is great for a lot of reasons. To tweak a value in soft code, we won’t have to read up into complex code, won’t have to change the value in multiple places and won’t have to deal with any magic values (values without a name to make it make sense).

Quality Code is Structured

Code that has high quality means that it is structured; it’s easy to understand because its structure makes sense. Take a look at the following example.

Not a code example this time! This chart represents a set of classes, we start off with the world class. A world has a set of cities, which each has one zoo. Each zoo has a set of animals, which can be either a bird type of fish type; sharks, ducks and maybe even more.

This set of classes is simple, because the hierarchical layout makes sense. No arrows are drawn in a nontrivial direction.

Structure does not stop at class associations. We can also put structure in the way we order the methods in our class. In the way we organize our script folders and where we decide to use a namespace. Good structures are easier to remember and allow us to focus on what really matters.

Last words

Did we forget a topic? We probably did; code quality has many factors. Although I love quality code, it is crucial to remember that in the end it’s all about making great games.

Sometimes our timeframe tells us to invest in code quality, other times it tells us to implement a cruel and nasty hack. But I believe that to keep progressing our level and that of our games it is essential to push the quality of our code throughout our life of programming.

Thank you for reading!

Read more about:

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like