SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function return values

    function getFirstLevelHeaders() {
    var h1s = document.getElementsByTagName('h1');
    if (h1s && h1s.length > 1) {
    return h1s;
    }
    return -1;
    }
    I've noticed people using return values of null and -1. I know the default value is undefined. Could someone provide insight on which should be used, if any at all? This is a simple function (which I probably wouldn't use in the real world) but its there as an example nonetheless. What advantages/disadvantages do I gain from using -1 as the return value? Or anything else? For when this function is called, or if a variable is assigned to this function.

    On a related note, would anyone use "else { return -1; }" instead of just having the if() and having a return of -1 after?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's all in how you check the result. This function isn't the best example, because I would expect it to return a collection of H1 tags, whether empty or not, which I could feed into a for loop (even an empty collection won't break a for loop but -1 would!). Normally if the function is an "exists" type function I would return true or false.

    If the function returned an integer zero or greater on success, then returning -1 would indicate a failure while keeping within the data type expected from the function. Here's one I made earlier:
    Code:
      Array.prototype.indexOf = function(testValue) {
        for (var i=0; i < this.length; i++) {
          if (this[i] == testValue) {
            return i;
          }
        }
        return -1;
      }
    This function, if successful, returns a value of 0 upwards indicating the index of the element searched for, but -1 if it wasn't found. The return data type is the same but since you can't have a negative array index, I used -1 to indicate failure.

    For your related question, you could use else, but why bother? If the condition was met, the function already returned and the code after the if doesn't get executed anyway. If you do bother to use else, then likely you should define a return value and use one return statement for the function.
    Code:
    var retVal;
    if (something)
      retVal = "1099";
    else
      retVal = "not";
    
    return retVal;

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    I've noticed people using return values of null and -1. I know the default value is undefined. Could someone provide insight on which should be used, if any at all? This is a simple function (which I probably wouldn't use in the real world) but its there as an example nonetheless. What advantages/disadvantages do I gain from using -1 as the return value? Or anything else? For when this function is called, or if a variable is assigned to this function.
    -1 is commonly used as a failure code, where any positive integer including 0 would signify the result of a successful operation.

    On a related note, would anyone use "else { return -1; }" instead of just having the if() and having a return of -1 after?
    Multiple return statements are one of my pet horrors, and no matter what anyone tells you they're bad news when it comes to debugging. I've heard all the counter arguments but all functions have a single entry point, and it should be just as clear where they end.
    Unnecessary use of the break statement is a very similar hazard.
    Tab-indentation is a crime against humanity.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    I consider that returns should be done at the very start of the function when you're testing the intial conditions, which helps prevent the code from being overly nested.

    The idea of not having more than one return point is a hangover from the days of machine coding, when multiple erturns really did your head in.

    If code is poorly written and it's hard to keep track of things, restricting the function to a single return is not going to be of much help.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •