54.8 F
Los Angeles
Saturday, November 23, 2024

Trump Lawyer Resigns One Day Before Trial To Begin

Joseph Tacopina has filed with the courts that he will not represent Donald J. Trump. The E. Jean Carroll civil case is schedule to begin Tuesday January 16,...

Judge Lewis A. Kaplan Issues Order RE Postponement

On May 9, 2023, a jury found Donald J. Trump liable for sexual assault and defamation. The jury awarded Ms. Carroll $5 million in damages. Seven months ago,...

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™...
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