Best WordPress Hosting
 

Examples of Refactoring PHP Code for Better Readability

via deliciousbrains.com => original post link

Examples of Refactoring PHP Code for Better Readability

Refactoring code is when you restructure existing code without changing its external behavior. Basically your aim is to make “bad” code better without changing the underlying functionality, and one of the key ways you can do this is by making sure human beings can understand it. In this article, we’ll look at some of the refactoring techniques you can use to make sure your code is clear, human-readable, and simpler to maintain.

It should be noted that there are cases when the time spent refactoring would be better spent rewriting the code from scratch. Minor tweaks and refactors can improve maintainable code, but gutting over 50% of your code may mean a rewrite is more cost-effective. Refactoring incrementally over time avoids crossing this threshold and makes that part of the code easier to maintain in the future. A previous version of this article was written by Gilbert Pellegrom and published in 2016. This version largely retains his original code examples.