54.9 F
Los Angeles
Saturday, December 9, 2023

ASUS Announces 2023 Vivobook Classic Series

On April 7, 2023, ASUS introduced five new models in the 2023 Vivobook Classic series of laptops. The top laptops in the series use the 13th Gen Intel® Core™...

Airman Arrested In Case of Leaked Classified Documents

Massachusetts Air National Guardsman Jack Teixeira has been arrested in the latest case of leaked classified documents. Teixeira, 21, is the head of Thug Shaker Central, an online gaming...

Microsoft OneNote to Gain Copilot

Microsoft 365 Copilot is coming to OneNote.
StaffPenguin Flight LogThe Importance of Refactoring PHP Code

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?

LPH
Layne Heinyhttp://www.layneheiny.com
LPH is a high school physics teacher interested in the Apple iPad and iPhone, Microsoft Surface, Tablet PCs, and other mobile devices. He resides with one large dog who begs for pizza, hamburgers, French fries, and anything else on the dinner table.

Latest news

Related news