Better Programming

Advice for programmers.

Follow publication

Member-only story

Data-Oriented-Design — A Swift Introduction

Most developers know how to design software around maintainability and OOP. But what if performance matters more than encapsulation and access levels?

Marcel Kulina
Better Programming
Published in
9 min readMar 21, 2023
Photo by Conny Schneider on Unsplash

The problem

Think about a game where we have hundreds or even thousands of units moving at the same time. All these units would need a way to know when they should move, where they can move, and much more. All these units have some behaviour in common: movement.

Note: The code in this article is just for explanation. It lacks many things and is untested. It is mainly intended to showcase what an implementation can look like.

Our first step here is to define a structure that holds the movement. We call it Vector3:

struct Vector3 {
// Convenience zero vector
static let zero = Vector3(x: 0, y: 0, z: 0)

var x: Float
var y: Float
var z: Float

init(x: Float, y: Float, z: Float) {
self.x = x
self.y = y
self.z = z
}
}

As a developer, this would be the perfect fit for a simple composition. So we define an interface.

protocol MoveUnit {
func move()
}

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (2)

Write a response

What I applaud in Mr. Carnevale's piece is an attempt to shift the attention away from affirmative action and college (now, there's a silo) to education reform of K-12 education. In my view, the problem starts when humans by and large support their…

Affirmative Action still around?

interesting