During Week 3 of Hacktoberfest, I decided to contribute to a smaller but promising project: an Online Auction System. Though the project is still in its early stages, it already shows potential for growth, and I saw an opportunity to help improve its codebase. My task was to refactor the project by reducing redundant code and improving overall structure, making it more maintainable and scalable.
The core objective of my task was to identify repetitive code patterns and refactor them to make the codebase cleaner and easier to maintain. Code redundancy can lead to inconsistencies, make future updates harder, and increase the likelihood of bugs. By streamlining the code, I aimed to improve efficiency without altering functionality.
1. Centralized Asynchronous Error Handling: One of the common issues in Express.js projects is repetitive try-catch blocks for handling errors in asynchronous routes. To address this, I introduced a utility function called asyncHandler that wraps all async functions, ensuring that any errors are automatically caught and handled consistently across the app.
// utils/asyncHandler.js const asyncHandler = (fn, errorMessage = "Internal server error") => (req, res, next) => Promise.resolve(fn(req, res, next)).catch((error) => { console.error(`Error: ${error.message}`); res.status(500).json({ error: errorMessage }); }); module.exports = asyncHandler;
This change not only removed redundant try-catch blocks but also made the code cleaner and more focused on the business logic, rather than error handling.
2. Database Utility Functions: The project had several routes that directly interacted with the database, often repeating similar queries. To reduce redundancy, I created a set of reusable utility functions to handle common database queries, such as fetching a buyer's information or retrieving transactions.
// utils/dbUtils.js const getBuyerById = async (buyerId) => { const query = "SELECT * FROM Buyers WHERE Buyer_ID = $1"; const { rows } = await pool.query(query, [buyerId]); return rows.length > 0 ? rows[0] : null; }; const getTransactionsByBuyerId = async (buyerId) => { const query = "SELECT * FROM Transactions WHERE Buyer_ID = $1"; const { rows } = await pool.query(query, [buyerId]); return rows; }; module.exports = { getBuyerById, getTransactionsByBuyerId };
Now, instead of writing SQL queries directly in each route, the codebase can rely on these utility functions, reducing duplication and making the logic easier to manage.
3. Unified Response Handling: Another area of improvement was response handling. Different routes had inconsistent patterns for sending success and error responses. To standardize this, I introduced utility functions like send404, send500, and sendSuccess.
Since the project is still in its early stages, I noticed some missing routes like POST, DELETE, and PUT that I could have easily create. However, I decided to stick with only my Issue was asking.
Let me know in the comments, how do you usually deal with this kind of situations that you could contribute more...Do you open a new issue, do you just create it and do a Pull Request? I would love to read you experience.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3