How do you structure programs in a language without classes?

0
1
Asked By MellowOrbit42 On

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.

5 Answers

Answered By CedarFox17 On

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.

Answered By SilverKite64 On

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.

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?

Answered By PixelHarbor31 On

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.

Answered By NorthwindLark5 On

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.

Answered By QuietMarble8 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.