1

I want to optimize my code to control multiples parameters into a function.

Like this example :

getEvaluation ( _val1, _val2, _val3, _val4) {
 let val_1 = _val1.trim(), 
     val_2 = _val2.trim(),
     val_3 = _val3.trim(),
     val_4 = _val4.trim();
 let validation = true;


        if ( typeof val_1 !== 'string' && val_1 !== null && val_1 !==  undefined && val_1.length > 0 ) {
            validation = false;
        }
        if ( typeof val_2 !== 'string' && val_2 !== null && val_2 !==  undefined && val_2.length !== 31  ) {
            validation = false;
        }
        if ( typeof val_3 !== 'string' && val_3 instanceof date ) {
            validation = false;
        }
        if ( typeof val_4 !== 'number' && val_4 < 1 && val_4 > 10 ) {
            validation = false;
        } 
return validation; 
}

What's the good best pratice for this example.

Seb
  • 404
  • 2
  • 4
  • 14
  • You can use the [**`arguments`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/arguments) object! – ibrahim mahrir Mar 16 '17 at 11:14
  • What is this supposed to do `if ( typeof val_1 !== 'string' && val_1 !== null && val_1 !== undefined && val_1.length > 0 )`??? – ibrahim mahrir Mar 16 '17 at 11:15
  • Possible duplicate of [Function overloading in Javascript - Best practices](http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices) – Pablo Lozano Mar 16 '17 at 11:52

2 Answers2

2

I've seen a lot of APIs that handle optional parameters in different ways, but I usually use the following rules (there may be exceptions):

If I have to pass many parameters, I wrap them in an object, so

function funcA(a,b,c,d,e,f) {
   //a lot of ifs to check each param
}

Becomes:

function funcA(paramObj) {
  if (paramObj.a) { //is "a" populated?
    ...
  }
}

With this approach there is no issues with optional parameters, you don't have to check if the first parameter is actually the second one because the first one was ommited. Besides, if you update your API adding or removing parameters your code can be easily backwards compatible.

But if my function just receives less than 4 parameters, I order them putting the mandatory ones first and the optional ones after:

function myFunc (a, b [,c] [,d]) {...}

By the way, your code has some issues:

  • calling trim() before checking if they are strings it is not a good idea
  • I guess you wanted to use || instead of && in your checks.
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
0

My suggestion to use one JavaScript object in function parameters instead of passing all of these params

Ahmad Rezk
  • 198
  • 1
  • 13
  • I know it is a lossing battle, but I'd like to remark that JSON and Javascript Object are not the same thing, the N means "notation" and "passing a JSON" usually means passing a string that needs to be parsed – Pablo Lozano Mar 16 '17 at 11:51
  • Thanks, Good remark. after I answered question I saw your answer and voted up for you :) – Ahmad Rezk Mar 16 '17 at 11:57