Code Smell 80 - 중첩된 Try/Catch
7609 단어 webdevoopexceptionstutorial
TL;DR: Don't nest Exceptions. Nobody cares of what you do in the inner blocks.
문제
솔루션
샘플 코드
잘못된
try {
transaction.commit();
} catch (e) {
logerror(e);
if (e instanceOf DBError){
try {
transaction.rollback();
} catch (e) {
doMoreLoggingRollbackFailed(e);
}
}
}
//Nested Try catchs
//Exception cases are more important than happy path
//We use exceptions as control flow
오른쪽
try {
transaction.commit();
} catch (transactionError) {
this.withTransactionErrorDo(transationError, transaction);
}
//transaction error policy is not defined in this function
//so we don't have repeated code
//code is more readable
//It is up to the transaction and the error to decide what to do
발각
구문 분석 트리를 사용하여 이 냄새를 감지할 수 있습니다.
태그
try {
transaction.commit();
} catch (e) {
logerror(e);
if (e instanceOf DBError){
try {
transaction.rollback();
} catch (e) {
doMoreLoggingRollbackFailed(e);
}
}
}
//Nested Try catchs
//Exception cases are more important than happy path
//We use exceptions as control flow
try {
transaction.commit();
} catch (transactionError) {
this.withTransactionErrorDo(transationError, transaction);
}
//transaction error policy is not defined in this function
//so we don't have repeated code
//code is more readable
//It is up to the transaction and the error to decide what to do
구문 분석 트리를 사용하여 이 냄새를 감지할 수 있습니다.
태그
결론
예외를 남용하지 말고 아무도 잡을 수 없는 예외 클래스를 만들지 말고 모든 경우에 대비하지 마십시오(커버링 테스트가 있는 좋은 실제 시나리오가 없는 한).
항상 예외적인 경우보다 행복한 길이 더 중요해야 합니다.
처지
Code Smell 73 - 예상되는 경우의 예외
Maxi Contieri ・ 2021년 5월 31일 ・ 2분 읽기
#webdev
#codenewbie
#cleancode
#oop
코드 냄새 26 - 예외 오염
Maxi Contieri ・ 11월 16 '20 ・ 2분 읽기
#oop
#exceptions
#tutorial
#programming
더 많은 정보
Code Smell 73 - 예상되는 경우의 예외
Maxi Contieri ・ 2021년 5월 31일 ・ 2분 읽기
#webdev
#codenewbie
#cleancode
#oop
코드 냄새 26 - 예외 오염
Maxi Contieri ・ 11월 16 '20 ・ 2분 읽기
#oop
#exceptions
#tutorial
#programming
더 많은 정보
학점
사진 제공: David Clode on Unsplash
영감을 준 @에게 감사합니다.
로드리고
✍️ Clean code tip try/catch추출 예외 처리 방식과 로직을 분리합니다. 여러 try/catch를 사용하면 코드의 의도가 흐려지므로 코드를 읽을 수 있도록 분리하는 것이 좋습니다.두 번째 그림에서 코드가 얼마나 개선되었는지 보세요.
오후 14:32 - 2021년 6월 11일
Writing software as if we are the only person that ever has to comprehend it is one of the biggest mistakes and false assumptions that can be made.
카롤리나 슈추르
소프트웨어 엔지니어링 좋은 인용구
Maxi Contieri ・ 12월 28일 '20 ・ 13분 읽기
#codenewbie
#programming
#quotes
#software
이 기사는 CodeSmell 시리즈의 일부입니다.
코드에서 냄새 나는 부분을 찾는 방법
Maxi Contieri ・ 2021년 5월 21일 ・ 4분 읽기
#codenewbie
#tutorial
#codequality
#beginners
Reference
이 문제에 관하여(Code Smell 80 - 중첩된 Try/Catch), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/mcsee/code-smell-80-nested-try-catch-3o5c
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Writing software as if we are the only person that ever has to comprehend it is one of the biggest mistakes and false assumptions that can be made.
소프트웨어 엔지니어링 좋은 인용구
Maxi Contieri ・ 12월 28일 '20 ・ 13분 읽기
#codenewbie
#programming
#quotes
#software
코드에서 냄새 나는 부분을 찾는 방법
Maxi Contieri ・ 2021년 5월 21일 ・ 4분 읽기
#codenewbie
#tutorial
#codequality
#beginners
Reference
이 문제에 관하여(Code Smell 80 - 중첩된 Try/Catch), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mcsee/code-smell-80-nested-try-catch-3o5c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)