Refactoring Code for Enhanced Maintainability: From In-file Data Objects to JSON
The Original Approach: In-file Data Object
Initially, the technology stack data for the front page of fzeba.com was embedded directly within the React component using a constant array cactusTech
. This array held multiple technology entries, each described by properties like desc
, href
, and title
. The array was directly used in the component to render the technology stack section.
While this approach worked, a better solution (for me) was to separate the data from the component code. Then load the data from a JSON file, which would make the code more maintainable and scalable.
Here’s a snippet of the original (React) component using the in-file data object:
The Improved Approach: JSON Data File
To address these issues, I refactored the code to load the technology stack data from a separate JSON file. This change provided several benefits:
- Improved Maintainability: Separating the data from the component code makes the system easier to maintain and modify.
- Enhanced Scalability: With data in a separate file, adding or updating technology stack entries is simpler and does not require modifications to the component code.
- Better Organization: The separation adheres to modern development practices, keeping the codebase cleaner and more organized.
The refactored code now loads the technology data from a JSON file, as shown below:
The data file techStack.json
looks like this:
Outcome and Benefits
The migration to a JSON-based data handling approach has made the codebase more flexible and maintainable. It simplifies updates and ensures that the application can scale more effectively as new technologies are added to the stack. This refactor not only improves the current state of the project but also aligns it with best practices for future development and maintenance.
Conclusion
Refactoring might require some upfront effort but is often worth it for the benefits it brings in terms of maintainability and scalability. By separating concerns and extracting the data layer from the presentation layer, applications can grow more seamlessly and remain manageable even as they evolve (totally not written by a AI - trust me).