Here is a hypothetical example:
I have a market.service.ts with 2 functions called buy() and sell() which are each 100 lines of code
export class ShopService {
public async buy() {
// 100 lines of code
}
public async sell() {
// 100 lines of code
}
}
I want to refactor these two into 2 main functions which each will call some mini functions. Each of these two are being called in a controller, so i can not change the public api for this service and buy() and sell() should have the same api.
Here is my question:
What are the pros and cons of each of these approaches? (for context, my main concern is code readability and not performance at all cost! also, this class has 100% test coverage)
Here are the two approaches i can take for this:
- I can define the mini functions as private functions inside this service class and just call them once.
export class ShopService {
public async buy() {
this.checkBuyMarket();
this.checkIfUserCanBuy();
this.makeTheBuyOrder();
}
public async sell() {
this.checkSellMarket();
this.checkIfUserCanSell();
this.makeTheSellOrder();
}
private async checkSellMarket() {
//30 lines of code
}
private async checkBuyMarket() {
//30 lines of code
}
private async checkIfUserCanBuy() {
//30 lines of code
}
private async checkIfUserCanSell() {
//30 lines of code
}
private async makeTheBuyOrder() {
//30 lines of code
}
private async makeTheSellOrder() {
//30 lines of code
}
}
- I can define these functions that are called only once, within buy() or sell() and call them there.
export class ShopService {
public async buy() {
checkBuyMarket();
checkIfUserCanBuy();
makeTheBuyOrder();
async function checkBuyMarket() {
//30 lines of code
}
async function checkIfUserCanBuy() {
//30 lines of code
}
async function makeTheBuyOrder() {
//30 lines of code
}
}
public async sell() {
checkSellMarket();
checkIfUserCanSell();
makeTheSellOrder();
async function checkSellMarket() {
//30 lines of code
}
async function checkIfUserCanSell() {
//30 lines of code
}
async function makeTheSellOrder() {
//30 lines of code
}
}
}
Both approaches have their pros and cons, and the choice between them often comes down to personal preference and the specific context of your project. But as you said your priority is code readability then I would say you should prefer Private Methods. It has code readability, and code can be reused and can also encapsulate.
Ensure that you're not introducing code duplication. If similar logic is needed in both buy and sell, consider refactoring common functionality into shared private methods. Also, a good practice is to use private methods for more complex logic and using inline functions for simpler steps.