Concept Explanation
Refactoring in Go is mostly about making code easier to read, test, and extend without changing what it does. In this lesson, you will start with a small function, then improve it by choosing clearer names, separating responsibilities, and keeping error handling obvious. The goal is not to make the code clever. The goal is to make it boring in the best way: easy to scan, easy to trust, and easy to change later.
Where to Put the Code
- Define color and position variables at the top.
- Create shape drawing or placement logic in the middle.
- Render output (print, canvas, SVG, or styled block) at the end.
Command Reference
- Run the starter code once, then rename anything that feels vague before you change the logic.
- Refactor in small moves: one naming change, one structure change, then test again.
- Check that the output stays the same after each edit so the refactor stays safe.
- Prefer simple functions with one clear purpose over packing everything into main.
Step-by-step Guide
- Run the original version first so you know the current behavior before touching the code.
- Look for one sign of clutter, such as a vague function name or mixed responsibilities.
- Refactor one piece only, then run the program again to confirm nothing broke.
- Compare the first version and the cleaned-up version and explain which one is easier to maintain.
- Finish with a short checklist: readable names, small functions, and unchanged output.
Practice Exercises
- Take a short function that mixes calculation and printing, then split those jobs apart.
- Rewrite a small example using clearer parameter names and explain why the new names help.
- Create a second version of the solution that is shorter but still easy for a beginner to follow.
Coding Challenges
- Refactor a slightly larger example with two helper functions without changing the final result.
- Improve the structure of the code while keeping the program easy to test from main.
Mini Practice Tasks
- Rename one function so its purpose is obvious without reading the body.
- Move one piece of repeated logic into a helper function.
- Write one sentence explaining what the refactored code does.
Common Mistake
Mixing x and y axes or using wrong coordinate origin causes shapes to appear in unexpected places.
Real-life Mini Challenge
Draw one square, one triangle, and one circle, then move X marker 2 steps right and 1 step down.