Challenge:
I was tasked with refactoring a complex legacy web application built on a PHPBB templating engine, originally developed in 2001. The system was highly inefficient and plagued by numerous issues, including:
- Code Redundancy: Each PHP file contained upwards of 1,000 lines of code, with heavy reliance on copy-pasted functions, violating the DRY (Don’t Repeat Yourself) principle.
- Database Overload: The application made a minimum of 300 database requests per page load, with files repeatedly connecting to the database (over 120 times), causing severe performance degradation.
- Nested Query Loops: Inefficient database queries, including nested loops, resulted in unnecessary load on the database.
- Bug-Ridden Operations: Critical tasks like payment processing were susceptible to bugs, including currency mix-ups and other inconsistencies.
- Excessive Ajax Calls: On certain user actions, Ajax calls triggered up to 500 concurrent database connections.
Solution:
Despite constraints from management, who wanted minimal intervention to prioritize other projects, I embarked on a comprehensive refactor to stabilize and optimize the system, focusing on what could be improved.
-
Refactoring the Codebase:
- I applied functional programming principles to identify patterns of code repetition and encapsulate them into reusable functions. This was essential given the lack of consistency in the app’s design.
- I explored Object-Oriented Programming (OOP) initially but found it difficult to implement due to the fractured and disjointed architecture of the existing codebase.
-
Database Optimization:
- I focused on minimizing the number of database requests by aggregating queries where possible.
- Developed a custom class to handle the dynamic detection and optimization of database queries. The class analyzed incoming queries, normalized them, and optimized them for minimal requests, ensuring only the necessary data was retrieved efficiently.
-
Performance Improvements:
- By reducing unnecessary queries and optimizing data retrieval, I managed to reduce page load times significantly.
- The custom class helped avoid redundant database queries and improved the consistency of data retrieval across the application.
-
Bug Fixes and Stabilization:
- Focused on fixing critical bugs, particularly those affecting payment processes and data consistency.
- Cleaned up numerous smaller bugs across the system, providing a more stable and reliable application.
Outcome:
While the project could not be fully revamped due to the constraints of the existing system, I made significant improvements in performance and maintainability. The application now operates with fewer database requests, more manageable code, and fewer bugs, which has resulted in a more stable and user-friendly experience.
Key Skills Used:
- Legacy Code Refactoring
- PHP, Functional Programming
- Database Optimization & Query Aggregation
- Dynamic Query Optimization
- Bug Fixing and Code Stabilization
Project Link:
Company Website – View the live website, where the refactor work was applied, improving performance and stability.