Refactoring code is a fancy way of stating that the developer re-writes an area of code. Refactoring can be as easy as pulling out a section of code and making it stand alone as a method or as complex as moving files and directories.
Refactoring can help debug code, improve the ability to unit test, and lead to future proofing a project.
I started refactoring sections of code in XenWord after realizing how difficult the code was to read. It all started with my attempts to comment code with PHP DocBlocks. Things really heated up when I decided to follow the DRY principle (Don’t repeat yourself) as well as agreeing to follow the single use principle in SOLID. A huge undertaking happened when I realized options were needed to turn on or off features which I grouped together called systems.
More important, Object Oriented Programming (OOP) is a very different way of thinking from procedural coding. It’s taken years for me to get past instantiating a class and differences between static and non-static methods. The use of $this, ->, and the :: operator only recently started to be clear.
Ironically, while trying to understand XenForo’s completeLogin method, I ran into a thread in which someone posted some of my original code. The code was horrendous and embarrassing. Seeing the code led me to start writing down key ways to write better code.
It turns out Rafael Dohms presented several of the principles about four years ago. His video is worth the time to view.
Here is a short list of the top ideas for me:
- Keep indentation to one level deep
- Avoid the else
- Keep classes small
- improve code commenting
Do you have a list, either written or in mind, while you code?