user1049057
user1049057

Reputation: 459

same return variable for different functions

I have lots of functions which is used for validations. Can I use same return varibale for all the functions?

function fn1() {
    if () {..
        first = true;
    } else {
        first = false;
    }
    return first;
}

function fn2() {
    if () {..
        second = true;
    } else {
        second = false;
    }
    return second;
}

function fn3() {
    if () {..
        three = true;
    } else {
        three = false;
    }
    return three;
}


$('#submitButton').click(function () {
    var returnVal = true;
    returnVal1 = fn1();
    returnVal2 = fn2();
    returnVal3 = fn3();

    if (returnVal1 && returnVal2 && returnVal3) {
        returnVal = true;
    } else {
        returnVal = false;
    }
    return returnVal;
});

Instead of these many variables like returnVal1, returnVal2 and returnVal3, can I use returnVal for all the function returns? Can I use a single variable for all the function returns?

Thanks

Upvotes: 0

Views: 81

Answers (5)

RahulOnRails
RahulOnRails

Reputation: 6542

function validateFunction(){   
  var result;     
  // First Function Condition\
  if(First Function)
      { 
        result = true;
      else
        result = false;
      }
      checkTruFalse(result);

  // Second Function Condition
  if(Second Function)
      { 
        result = true;
      else
        result = false;
      }
      checkTruFalse(result);

   // Third Function Condition
   if(Third Function)
       { 
         result = true;
       else
         result = false;
       }
       checkTruFalse(result); 
   }
 }

 function checkTruFalse(result)
   {   
     if (result == false ){ alert('ERROR'); break; } 
   }

 $('#submitButton').click(function(){ validateFunction(); });

As per the situation you can pass another variable inside checkTruFalse function to set alert message about specific error.

function checkTruFalse(result, message ){
  alert('ERROR :: '+message);
}

checkTruFalse( result, 'Invalid Input for Numeric Value');

Upvotes: 0

megawac
megawac

Reputation: 11383

If your returnValues are primitives that are going to be combined, you can easily combine their assignments. In your code snippet you can write it as below if you'd prefer:

$('#submitButton').click(function () {
    var returnVal = first() && second() && third();

    return !!returnVal; //ensure boolean is returned
});

Upvotes: 1

musefan
musefan

Reputation: 48465

Yes you can, but if you want a single end variable that determines if any single one fails then I would recommend doing so like this:

var isValid = first();
if(!second())
    isValid = false;
if(!three())
    isValid = false;

That way you don't end up resetting an invalid flag with a subsequent result.

The benefits of this, is that if you have a lot of validation functions you can throw them into an array and write less code...

var validators = [first, second, three];//etc..
var isValid = true;
for(var i = 0; i < validators.length; i++){
    if(!validators[i]()){
        isValid = false;
        break;
    }
}
//do something with isValid

Or if you have a dedicated "validate all" function...

var validators = [first, second, three];
function validateAll(){
    for(var i = 0; i < validators.length; i++){
        if(!validators[i]()){ 
            return false;
        }
    }
    return true;
}

Upvotes: 1

Sirko
Sirko

Reputation: 74106

You can drop the variables all together and just call your validation functions inside the if clause:

$('#submitButton').click(function () {
    if ( first() && second() && third() ) {
       return true;
    } else {
       return false;
    }
});

or shorter as @FishBasketGordo suggested, if the return value is the only thing you need:

$('#submitButton').click(function () {
    return first() && second() && third();
});

If the validation is rather computation heavy, this also saves you some time, as the if-clause is evaluated lazely.

Upvotes: 4

Pascamel
Pascamel

Reputation: 953

You can replace the last function by the following code

$('#submitButton').click(function(){
    return (first() && second() && third());
});

Upvotes: 1

Related Questions