See all glossary termsDon't Repeat Yourself (DRY) is a popular principle in software development that aims to reduce repetition in code. The DRY principle states that "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."
In simpler terms, you should avoid duplicating data or logic in your project. Instead, you should group important data and logic into components or locations that are accessible by all the systems that need it in your project. This way, if you need to make a change, you only have to update it in one place, reducing the chances of introducing inconsistencies caused by duplicated elements.
For example, if you have items in the game, you should have a single database, a single place, or a file that knows all the types of items in the game. This way, your shop, inventory, and dialogue system can all access the same database. When designers add or remove items, everything can access the updated data.
DRY is a very sound idea but one you don't want to take too far. Practically speaking, you don't want to make every small piece of code generic and reusable just to avoid repetition. Doing so can lead to code that's more abstract and difficult to understand as a result.
A good rule of thumb is that if you repeat the same code three times in your codebase, you may consider refactoring it into a reusable function or module. However, if you only use a piece of code once or twice, it's often better to keep it where it is to avoid over-engineering your solution.
Also, when you have two scripts that look similar but are not quite the same, it's often a good idea to keep them separate rather than making shared code that'll likely be more complex to accommodate the slight differences.
I apply this principle in gameplay code by considering a "piece of knowledge" as one of the game's mechanics or entities.For example, if I work on an indie game with different flying mobs that have unique movement and attack patterns, I will not necessarily try to make a generic FlyingMob
class that can handle all of them. Unless the game is so large and involves so many people that it calls for a reusable system.Having unique code for the patterns of each mob can make the code more readable and much easier to debug and maintain as the logic is specific to that mob. Nathan
Founder and teacher at GDQuest
As with many general programming principles, you must use your judgment and experience to decide when to apply the DRY principle and when to avoid it.
The goal is to avoid a situation in which a single component of your codebase is repeated in multiple places and, when changed, requires updating in multiple instances.
This is error-prone because as the codebase grows, it's too easy to forget to change one of the instances, which can lead to bugs that are hard to track down.See Also
Related terms in the Glossary
school.gdquest.com - 0.3.5-2024-11-01T20:19:19.321Z-06c546de2e802c845fb21e0c367cf9cc56c07f79-release