숨은 로직 드러내기 | Frontend Fundamentals #64
Replies: 3 comments 7 replies
-
|
안녕하세요! 그래도 반환타입을 명시해주는게 좋을까요? 제가 생각했을때 반환타입을 적어주면 좋은 이점은
정도인데.. async function fetchBalance(): Promise<number> {
const balance = await http.get<number>("...");
return balance;
} |
Beta Was this translation helpful? Give feedback.
-
|
이 문서에 대해서 질문이있습니다. 이전 코드 async function fetchBalance(): Promise<number> {
const balance = await http.get<number>("...");
logging.log("balance_fetched");
return balance;
}여기서 두가지 문제를 언급하셨는데
그러면서 아래처럼 개선을 보여주셨는데요, 개선 코드 async function fetchBalance(): Promise<number> {
const balance = await http.get<number>("...");
return balance;
}
// 필요한 경우
const balance = await fetchBalance();
logging.log("balance_fetched");
await syncBalance(balance);개선 코드가 단순히 로깅을 빼고 필요한곳에서 하신다는게 의아합니다. 차라리 |
Beta Was this translation helpful? Give feedback.
-
|
저는 이런 숨은 로직 문제를 피하기 위해 로깅을 고차함수로 분리하는 방식도 괜찮다고 생각합니다. function withLogging<T extends (...args: any[]) => any>(fn: T) {
return async (...args: Parameters<T>): Promise<ReturnType<T>> => {
try {
const result = await fn(...args);
logging.log(`${fn.name}_called`);
return result;
} catch (error) {
logToSnapErrors(error);
throw error;
}
};
}이런 식으로 숨은 로직을 만들지 않으면서도 재사용성과 관심사 분리도 챙길 수 있는 것 같아요. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
숨은 로직 드러내기 | Frontend Fundamentals
변경하기 쉬운 프론트엔드 코드를 위한 지침서
https://frontend-fundamentals.com/code/examples/hidden-logic.html
Beta Was this translation helpful? Give feedback.
All reactions