Refactoring

Tutorial

·

Beginner

·

+60XP

·

90 mins

·

Unity Technologies

Refactoring

Professional teams schedule some time for code cleanup after each milestone so their code stays flexible and easy to work with. This tutorial will give you some tips and general principles for this process, known as refactoring.

With your vertical slice done and your code in good shape, this is where you’ll start down the road to alpha.

Languages available:

1. What is Refactoring and Why do I care?


2. Making Code More Legible by Renaming Variables


Optional Step

3. Intermediate Coding Techniques

You can make your code flexible and clean at any level of scripting knowledge, but certain coding techniques and C# features make it easier to design a good architecture, particularly as your projects get larger.






  • C# interfaces



  • Scriptable Objects



  • Inheritance and Composition

  • SigTrap: Unity Software Design- Inheritance and Composition



4. Refactoring Examples

We created a Unity project showing detailed examples of the code cleanup discussed in the videos, as well as simple working examples of intermediate-level code structures like coroutines. Go to the Tutorial Materials at the top of this page and download the zipped folder to see the examples.


5. Assignment: Refactoring Plan

It's time to tidy up your code so you're on a firm foundation for the rest of the project.


This assignment is about cleaning up code that you have already written. It serves two important purposes:


1. It will make your life easier as a developer for the rest of the project.


2. Going back through code and thinking about how to make it better is a key part of getting better at coding.


It’s never too early to refactor – you can rename variables, write comments and turn repetitive code into functions at any point. But this is a great moment in this project to step back and look at the structure of your code. You’ve been working on the project long enough to have run into some issues, but it’s not right before the final deadline when a refactoring would be risky.


On a pro team, you’d write up your refactoring tasks and put them in the sprint plan for that week. But this time, it's OK to just go through your code and fix issues as you find them rather than write them up as tasks. Prepare to spend 1-2 hours on this assignment as a separate block of work from your usual planning and development cycle.


Back up your project


Before you do anything else, back up your project! Close Unity, zip the entire project folder and put the zipped file somewhere safe, perhaps on a USB key or in Dropbox. It’s recommended to do this even if you’ve been using source control – you’re going to break your project while refactoring and you’ll want to have an easy way to un-break it.


Improve clarity


Take a few minutes to refresh your knowledge of how your code currently works. Open each of your script files, starting with the smaller ones. Can you tell what each line is doing? Are there places where you pasted in someone else’s code without fully understanding it?


As you read, look for places you can make the code clearer. Think carefully about how you are actually using your variables, and whether you could rename them. If you’re using a hard-coded number, try to turn it into a variable. Go ahead and make these changes as you see them.


If there’s a line or set of lines doing something complicated, put a comment above them briefly explaining what’s happening. If it’s three or more lines, consider moving them into a function so that you can give it a descriptive name. Go ahead and do this where appropriate.


Keep an eye out for code that’s doing basically the same thing in several places. Think about how you might turn those repeated lines of code into a function, or consolidate them by using a variable instead of hard-coded values. Make a list of these places.


Improve structure


Look at each of your classes again. For each one:


1. Can you describe everything the class does in one short sentence or 3-4 bullet points?


2. If so, go ahead and write those in a comment at the top of the class.


3. If not, think about how to fix it.


  • Look for parts of this class that would fit better in another existing class. Write down or draw a sketch of what you would move and where it would go.

  • Consider splitting the class into smaller classes. Sketch what that would look like.

For example:



Look ahead


Think about the remaining features you’re going to build. Is your code ready? For example:


  • If you’re planning to let the player restart a level, do you know what you’d need to reset (e.g. doors that have been opened) and what you’d need to remember (e.g. number of lives remaining)?

  • If you’re planning to have several kinds of enemies, but so far you’ve only built one, what will be different between them? Does your code make any assumptions that are based on that one enemy, e.g. how much damage it does?

Give some thought to how you would need to change your code to make these new features work. Often (though not always) you can accomplish this by adding new variables to your classes or adding new parameters to your functions. Write down notes or draw a sketch of new classes and/or how your existing code would need to be rearranged.


Prioritize and (optionally) discuss


When you’re done with the steps above, you will have a to-do list describing ways that you can improve your code. You don’t have to do everything on it! You’ve already done yourself a big favor by going through these steps. But there’s probably at least a couple of things you know you need to fix before going into the final weeks of the project. This is the right time to do it.


This is also a great time to talk with your classmates about ways to improve your code. Explaining your code to someone else will help you to see it clearly and make smart adjustments. Besides, reading and understanding someone else’s code helps you get better at writing code yourself.


Here are your next steps:


  1. Make a plan using the instructions above. For reference, there are Refactoring Examples provided in the Tutorial Materials.

  1. Make a pdf showing your rough to-do items and any sketches you’ve drawn. Upload it for this assignment.

  1. Ask a question or offer a tip in the Gallery for this assignment.

Please login to submit

Complete this tutorial