What I learned about code while creating an ECS
Tags: architecture, codequality, ecs, csharp
Published: 2021-01-24 19:19:07

# Backstory

I've been a very opinionated developer from the beginning. My passion has been to figure out what actually makes development easy and promote/do that.

After working in an enterprise scale application for a couple years, I found a pattern that was really excellent for making that code simple and named it [ExtendedIPO](https://github.com/winstonpuckett/ExtendedIPO). While my team agreed that my code was incredibly readable, easy to extend, and easy to debug, there were members who had their own styles and thoughts about how to structure the code in this code base. I gave up and wrote [this post](https://winstonpuckett.com/posts/extendedipo/) on the subject instead.

I **knew** that I was right. That this was **the** way to code, and if everyone just followed the examples, there would be less bugs and things.

Then I wrote an ECS.

# Everything changed in one method...

If you don't know what an ECS is, [Catherine West explains it really well.](https://www.youtube.com/watch?v=aKLntZcp27M)

This was the first "large" project I worked on after I discovered [ExtendedIPO](https://github.com/winstonpuckett/ExtendedIPO). There are a couple code choices I had adopted since ExtendedIPO such as:

- Complete immutability
- list.ForEach() over foreach(var item in list)
- Composition over Inheritance
- [No tunnelling](https://youtu.be/AnZ0uTOerUI). You can only go 3 scopes deep

All of these go beyond ExtendedIPO to create consistent, readable code.

When I made the ECS World class, all of these choices came into question in one method.

When I call, "World.Start()", it iterates through all of the systems and calls the Execute method on each to mutate the components there. Each Execute method returns a "SystemResult" which exits the foreach loop when SystemResult = SystemResult.Exit.

Here's how it looks:

```C#
public void Start() 
{
    var systemResult = SystemResult.Continue;
    // Continue until a system asks to stop.
    while (systemResult == SystemResult.Continue)
    {
        // Perform one loop.
        foreach (var system in _systems)
        {
            systemResult = system.Execute();
            if (systemResult != SystemResult.Continue)
                break;
        }
    }
}
```

Here's every reason this goes against my list of "good" code choices:

- systemResult is mutable, but there's not a great way to make this readable otherwise (open for discussion)
- foreach gave 100ms better performance than .Foreach()
- _systems holds a list of type BaseSystem. This is inherited because each BaseSystem holds a bit of information about itself and must be constructed the same way each time. Of course, this is supposed to be shallow inheritance. Nothing should need to derive from something deriving from BaseSystem.
- I've put a foreach loop inside of a while loop.

# "Every methodology becomes religion"

This is a quote from a programmer I really respect. What it means is that everything from [OOP](https://en.wikipedia.org/wiki/Object-oriented_programming) to [Functional](https://en.wikipedia.org/wiki/Functional_programming) to [TDD](https://en.wikipedia.org/wiki/Test-driven_development) to [DDD](https://en.wikipedia.org/wiki/Domain-driven_design) eventually becomes something people believe has to be the **only** way to program.

At the point when a methodology has a following that refuses to program any other way, it has become a "religion." I've been hesitant to subscribe completely to any methodology because of this, but I've been searching for the best way to program nevertheless.

I realized that I had done what others have done before me when I created ExtendedIPO. While it's a great way to structure things in my codebase, declaring that it's the only way to program is just not right. All of these other things which I built on top of it is not the only way to program either.

# Instead...

We need to realize that all coding strategies are second to the domain. This is less structured than DDD. It's, explore and know your domain and write code which describes that domain well.