Coding Lessons…from Factorio?
Tags: coding, gaming
Published: 2019-03-21 01:23:40

---
title: Coding Lessons…from Factorio?
published: true
tags: coding,gaming
canonical_url: https://www.codingblocks.net/programming/coding-lessons-from-factorio/
---



[Factorio](https://store.steampowered.com/app/427520/Factorio/) is a game where you are a teeny tiny engineer has been stranded on a very large and hostile alien world. Your goal is to build a rocket and shoot yourself out of there, but that’s going to take some serious work!

<iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen frameborder="0" name sandbox="allow-scripts allow-same-origin allow-presentation allow-popups" scrolling="no" src="https://www.youtube-nocookie.com/embed/ndZBvwJkq3I?iv_load_policy=3&amp;modestbranding=1&amp;rel=0&amp;autohide=1&amp;playsinline=1&amp;autoplay=0" width="480" height="270"></iframe>

{% youtube ndZBvwJkq3I %}
<figcaption>Check out the video version of this post for some visuals.</figcaption>

You start with just a few basic tools and materials, and you can use those to build better tools and accumulate more materials all while trying to fend off hostile aliens lovingly called [biters](https://wiki.factorio.com/Enemies).

Now that doesn’t sound so different from other mining, crafting games, but the big difference here is in the scale.

![](https://www.codingblocks.net/wp-content/uploads/2019/03/Behemoth_biter.png)<figcaption>The aliens don’t take kindly to your pollutin’ ways!</figcaption>

Much like real life – building a rocket by hand, from scratch is impossible. Not only would it be time prohibitive to manually create every nut and bolt you would need, but there are some things that you simply cannot make with your own two hands like battery acid or petroleum products.   
Your only hope is to automate the mundane tasks as you bootstrap and leapfrog your way to a highly scalable, and robust distributed ~~factor~~ y system.

Hey, that sounds a lot like programming!

* * *

## **How is Factorio like programming?**

Much like programming a highly scalable, distributed system – it’s hard, and you are going to make mistakes, and you are going to spend a TON of time refactoring.

And remember the hostile aliens? They aren’t so bad in the beginning, but as the game progresses they become more and more aggressive. which adds some tension and urgency to your situation, and it means that you are essentially forced to sometimes make short-term tactical decisions that conflict with your larger strategic goals. These decisions help get you through your short-term problems, at the cost of increased pain in the future. This phenomenon, is known to programmers as “Technical Debt” and in many ways, Factorio is really a game about managing Technical Debt.

Sounds like great fun, right? …but really, it is! Let’s start with those biters!

* * *

###   
**#1 Security by Design, Development, and Deployment / Defense in Depth**

If you’re playing with aggressive biters enabled then you need to start thinking about security before you need it. If the aliens are able to skirt or overwhelm your defenses it can be really difficult to recover because you need to balance your time between fortifying your factory, repairing the damage, and keeping your resources flowing.

Life is much easier if you start thinking about security early in the process, because it’s can be difficult or even impossible to recover.

### **#2 Designing for scale**

Much like lesson #1, a little forethought upfront can save you a lot of time down the road when it comes to scaling.

Once you get your basic materials automated, it’s tempting to just fit belts, furnaces and assemblers in where it’s convenient. This works out great in the beginning because it takes a minimum of resources but the routing you need to do gets worse with each addition. A design like the one below is much better, because you can strap on additional components as needed to scale up the factory without having to do any plumbing.

The lesson here, is that it’s important to design for scalability before you need it. If you wait too long, and focus too much on tactical decisions over strategic ones you’ll quickly and frequently find yourself boxed in.   
Which brings me to another design time lesson:

### **#3 Modularity and Abstraction**

As your factory grows, you will eventually start running your initial resource pools dry. If you haven’t properly accounted for this inevitability, then it can be difficult to resupply your factory as you add new pools of resources. It’s typical for new players to build their factories around their initial resources. It evolves that way organically because it’s a cheap and convenient strategy early on but it causes great pain and crappy hacks to get around mid to late game as you need to spend a lot of time building in “clever” routing to wiggle resources into your center. If these hacks become bottlenecks then they can be really difficult to work around without changing large portions of your factory.

A better approach is to separate your inputs, logic, and outputs so you can scale and route them independently. This insulates you from having to make changes later because of future expansions.

This one reminds me of the [open-closed principle](https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle), designs should be open to extension but closed to modification

![](https://www.codingblocks.net/wp-content/uploads/2019/03/train.jpg)<figcaption>Trains are high bandwidth, but high latency</figcaption>

Speaking of making changes, the next three lessons deal with techniques for making changes

### **#4 Profiling is important**

It can be difficult to see supply chain problems before you suffer an outage, and one outage can often cascade into others. so it’s important to use the “P” screen periodically check on your productivity and consumption rates. If you are using a resource faster than you can make it, then you are eventually going to suffer outages because of it.

Usually you want to scale out to meet production demands, but as resources, space and time are limited it’s important to focus on things in the right priority.

### **#5 Safe refactoring**

[Safe refactoring](http://wiki.c2.com/?SafeRefactoring) refers to small and discrete changes that make things better without breaking the system. It can be tempting to rip up large swaths of our factory in order to do things “right” but if you go too far down the rabbit hole then it can be difficult to get things back to a stable state.   
This can be especially bad if the biters start heating up while you’re redoing the power grid.    
Same thing applies to programming, it’s much less risky to make small isolated changes and slowly work your way towards a larger goal. Especially if you have deadlines to meet!

### **#6 Managing Technical Debt**

It’s easy for me to say that you design for scalability up front. Turns out, that is really difficult. You are severely resource constrained in the beginning of the game. If you try designing your factory for the end game at the beginning, then you’re going to need a lot of resources up front to lay the proper infrastructure. Trouble is, it’s hard to gather a lot of the resources you need until you’ve moved up the research tree.

The trick is balance your factory between your current needs and your future plans. It’s a tough balance to get right, much like managing technical debt when you are coding.  If you spend a lot of time up front trying to design the perfect system, you run the risk of running out of resources before you start providing value. Additionally, it’s nearly impossible to fully plan a large system out ahead of time so it’s difficult to make correct decisions too early in the process. That’s why it’s important to try and keep a good balance.

Taking on technical debt is a great way to start delivering value early, the trick is to be smart about when you repay those debts. 

* * *

So woah, those are some pretty heavy lessons!  
You aren’t going to learn any syntax or fancy from playing Factorio, (maybe if you start looking at the making mods!) but I do think there are some valuable higher level lessons we can get some experience with by playing the game.

And there ya have it folks that’s how you can justify spending time at work playing Factorio. If your boss gives you a hard time, just tell them that you’re working on these six skills and everything will turn out great.  
Let me know what you think in the comments and if you like content like this, make sure to check out the Coding Blocks podcast!

![https://www.codingblocks.net/](https://www.codingblocks.net/wp-content/uploads/2019/03/cb.gif)