See all glossary terms

Refactoring

Refactoring means cleaning up and reorganizing your code without changing its functionality.
Imagine your desk full of pens, paper, dirty coffee cups, you name it. You have all the tools you need to work productively, but they're scattered everywhere. It's hard to find the things you need and to understand what's going on.
Code can get in the same state.
Refactoring is taking the time to tidy everything up and put everything in the right place. It doesn't change what you have, but it makes everything easier to use and understand.
There are a few reasons you might want to refactor your code:
  • Make it easier to read and understand: Clear, well-organized code is more accessible for you and other programmers to work with. It saves time and reduces the chance of bugs.
  • Make it faster to change and maintain: You'll need to update your code as your game grows and changes. Tidying up or simplifying the code makes adding new features or fixing bugs easier without breaking other parts of your game.
  • Improve performance: Sometimes, refactoring helps improve your game's performance by making your code more efficient or allowing optimizations that weren't possible before.

How refactoring is done

Refactoring can involve a large set of techniques and practices. Here are a few common ones:
  • Rename variables and functions: Choose names that are clear and descriptive.
  • Break down long functions that are difficult to reuse into smaller ones that can be composed.
  • Merge multiple small functions that don't work independently into a larger one.
  • Combine duplicated code into functions to reduce repetition.
  • Split code into multiple files to make it easier to find and manage.
  • Merge split code into a single file to reduce the spread and complexity of the code's interactions.
Notice how some of these techniques involve breaking code apart, while others involve bringing it together. As with everything in code, the most appropriate approach depends on the context and takes practice to get right.

See Also

Related terms in the Glossary