Comment

Comment by bjacks

I think that if the author re-wrote the first method in a cleaner way, composed of lots of smaller methods with descriptive names then the code would be a lot more readable, and you wouldn't even need to debate the whole multiple returns versus single point of return thing because your "mental stack" could handle either option equally well.

For example:

  public string Execute(int integer, string str, bool boolean) {
    string result;

    if (isValidInteger) {
       result = validateStringAndBool(str, boolean);
    } else {
       result = “Error: Integer is too small”;
    }
   return result;
}

  private string validateStringAndBool(string str, bool boolean) {
     if (!string.IsNullOrWhiteSpace(str)) {
       return isValidBoolean(boolean)          
     } else {
        return  “Error: String is null or empty”;
     }
  }
 
  private string isValidBoolean(bool boolean) {
     if (boolean) {
        return “Success”;
     } else {
        return “Error: Incorrect Boolean”;
     }
  }
And yeah, I can't see how this has anything to do with tail call optimization other than a fluffy analogy to stacks - mental and programmatic.
parent bjacks 11 years ago

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton