One common challenge for new coders is learning how to properly structure their code. Most often, beginners would add all their code in a single file or function. This practice leads to unstable code and making any update on the existing code very difficult.
In this post, let’s learn how to properly structure your code by designing a simple version of the popular Wordle game. If you’ve never played Wordle, check it out here.
Before writing any code, consider both the user flow and data flow. This will help you understand how to structure the code efficiently.
Here’s a rough outline of the game’s functionality:
We can create the following files or services based on the game flow we have decided:
Let’s add the following functions to our services:
GameEngine
UserInput
GameResult
This is a good starting point to make your code modular. We can see that there is no overlap between any of the functions. If we modify the code in any of the above functions, it won’t impact the others. For example, if I modify the saveUserScore function to save in a different database, it wouldn’t require re-testing any of the other functions.
Blog|Youtube|Careers|Contact Us
Have Feedback or want to contribute? Email: hello[@]100DaysOfCode.io
100DaysOfCode@2024