47.3 F
Los Angeles
Monday, January 30, 2023

Apple March 8, 2022 Event

Apple announced several products during their March 8, 2022, event. Studio Display Mac Studio iPad air iPhone SE iPhone 13 and 13 Pro color addition Some of the products will...

Eastman files motion for exculpatory information and continuance

In response to the January 6 Select Committee Brief to Eastman Privilege Assertions, Eastman has filed a new motion with the court. A request for the court to require...

February 2022 Employment Report

U.S. Bureau of Labor Statistics reported today that total nonfarm payroll employment rose by 678,000. The unemployment rate edged down to 3.8 percent The employment number exceeded forecasts The...

The Importance of Refactoring PHP Code

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:

  1. Keep indentation to one level deep
  2. Avoid the else
  3. Keep classes small
  4. improve code commenting

Do you have a list, either written or in mind, while you code?

Related Stories