Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add New Section To Functions - Prefer Exceptions to Returning Error Codes #79

Open
ofirbarak opened this issue Feb 5, 2020 · 0 comments

Comments

@ofirbarak
Copy link

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
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant