Member-only story
Coroutines, The Old 💎 Gem That Keeps Making Complex Code Simple
Understand a simple old pattern that is right below our noses and makes development far more easy.
This article is a celebration of an old technique that we are still enjoying. We’re in the 80s, computers have few resources, and even less memory. The dominant language is C, and every byte and every instruction counts, and every trick is welcome. And now, it turns out your code has to deal with multiple overlapping behaviors that add more complexity. Everything suggests the code will be very messy to squeeze out every last hertz. But that’s not the case, the code looks clean and charming. They knew advanced patterns that seem like magic.
This was the era when programmers had to become wizards.
And this magic still endures today.
A well-known example
Imagine you’re making a little game, and each game element has a behavior. Crystals that shine, slimes that crawl on the ground, and bats that want to eat them. These behaviors are simple, and if we wanted to write them, we would want to do something as simple as:
# Crystal pseudocode
def crystal_behavior(self):
while True:
# Bright state
self.active = True…