Add New Section To Functions - Prefer Exceptions to Returning Error Codes
Author: ofirbarakCreated Feb 5, 2020Updated Feb 5, 2020
Based on "Clean Code" book, I find this section very useful. Use exceptions to indicate on error occurred instead of returning error codes, it complicates the caller function's code.
// bad
int func1(){
if (some condition)
return -1; // Error
return 0; // Ok
}
void func2(){
if (func1() == 0)
return;
}// good
void func1(){
if (some condition)
throw new Exception(); // Error
return; // Ok
}
void func2(){
try
{
func1();
} catch (Exception ex) {
// Error
}
}Source: thangchung/clean-code-dotnet