How do you structure programs in a language without classes?
Asked by
MellowOrbit42
I have mostly used C++ and C#, so I'm accustomed to organizing code around classes and methods. I'm interested in learning Jai, which does not use traditional classes, and I'm wondering how that changes the way programs should be designed. What concepts or resources would help me learn procedural, modular, or data-oriented code organization? I'm especially interested in applying these ideas while eventually building a game engine, even though I know that will be a challenging project.
Answers
Modules and namespaces provide much of the organization that classes are often used for. Put related types and functions into a coherent module, expose a small public interface, and keep implementation details private where the language allows it. You still get encapsulation without requiring a class hierarchy.
Reply to this answer
Try focusing on the operations and data transformations your program needs instead of modeling every concept as a real-world object. Functions can accept structs or references to structs, and the caller only needs to know the relevant types and function signatures. Practicing this in C++ with structs and free functions is a good transition.
Reply to this answer
For a game engine, look into data-oriented design and Entity Component System architecture. Rather than making a deep hierarchy such as `Animal -> Bird`, an entity can hold components like position, movement, or health, while separate systems operate on entities with those components. This favors composition, reuse, and straightforward data access over inheritance.
Reply to this answer
Polymorphism isnโt required for every design. You can use explicit functions for different types, generic functions, tagged unions, function pointers, interfaces, or compile-time techniques depending on what the language supports. The tradeoff is that behavior may be less implicit than a virtual method call, but the design can also be easier to follow and debug. Also keep in mind that the language and toolchain are still evolving, so learn it as an experiment rather than relying on it immediately for career-critical work.
Reply to this answer
Start with C or another procedural language. You can define data with structs and keep the operations on that data as separate functions. For example, instead of calling `node.next()`, you might write `next(node)`. The data and behavior are still organized; they just arenโt bundled into one class.
Reply to this answer
Add your answer
Answers are reviewed before publication.
MellowOrbit42: If thereโs no inheritance, how would you handle something that can behave in several different ways? Would you store a component or function that represents the behavior and check for it when processing the entity?