Refactoring Code Helps While Learning To Code

Johnny Hacking
2 min readMay 21, 2021

Starting out on the development journey is like a rollercoaster. At first, it climbs up slowly to the very top, and then a rush of adrenalin hits you as your flying down the track. From there, it’s ups, downs, twists, and turns. Like the rollercoaster, my journey feels like the same way where at first, it was a slow climb as I learned basic concepts and how to think differently, but as I got past that first climb and rushed down the track was when I built my first simple app of hello world.

Now, as my and others’ journey or ride continues, one key thing will come into play: how do we learn? As developers, we can’t remember everything about programming, but if we know how to learn when it’s time to implement something new, we don’t know how to. Then the opportunity to learn comes.

There are tons of books on learning or how the brain can learn new things, but I found that to help drive home and learn programming concepts through refactoring code.

Refactoring is a technique for restructuring code, alterings its internal structure without changing its external behavior. It’s replacing complex code with a more straightforward piece or taking two blocks and making it one.

Here are a few benefits to refactoring code:

  • Improves execution time
  • It cuts down on file size for smaller downloads
  • Help find bugs
  • Easier to understand code
  • It helps us to program faster

Here is an example of a piece of code that runs and works perfectly.

Now here is the same code after it’s been refactored.

With both examples of code, there isn’t anything wrong. They both work and get the job done. In the refactored example, there are fewer lines of code, and I learned some new concepts to make the code better, including in the process, become a better programmer. For myself, like someone learning to program by trying to make this code better, refactoring pushes me to learn new concepts I am uncertain about or feel my understanding is lacking. By refactoring it causing us to find better ways to do things and, in the process, learn or understand new concepts that will help us become better programmers.

Happy coding!

--

--