0

I have a large code base, and I am tasked to implement additional functionality. Code is Node.js - Javascript.

There is one function, and it goes like this:

function checkPeople(first, second) {
 if(second) {
   //do some things...
   }
}

That same function is called several times, but generally it is called like:

checkPeople(first);

or...

checkPeople(first, second);

It depends what is being used for. the parameter "first" is always send regardless.

Now I have to add extra functionality to the same function but with the third parameter:

function checkPeople(first, second, third) {
  if(second) {
   //do some things...
   }
  if(third) {
   //do some things
   }
 }

I was thinking to add third parameter to the function, wherever the function is being called (around 2 times), and check for the existance of the parameter inside the function. Which it is already done for the the parameter "second".

Is this approach valid?

Thank you

Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • 2
    Yes,it is a valid approach. – misss-popcorn Jul 25 '16 at 09:00
  • 1
    @Wexoni Based on the `checkPeople` function, this could work, yes. Does it have to do the same check for every person or does it have different functionality based on the parameter ? – DoXicK Jul 25 '16 at 09:01
  • This is how it's usually done. Or sometimes they add default values – Bálint Jul 25 '16 at 09:01
  • Function has same functionality, it is only small part that is doing something based on the existance of second or third parameter... – Amiga500 Jul 25 '16 at 09:02
  • You may want to read [How to overload functions in Javascript](http://stackoverflow.com/questions/10855908/how-to-overload-functions-in-javascript/10855939#10855939). – jfriend00 Jul 25 '16 at 09:27

2 Answers2

1

If you pass in the argument like this when you call the function, using null as the second argument, it will not call the second. Assuming that is what you are trying to do. checkPeople(first, null, third)

alex
  • 5,467
  • 4
  • 33
  • 43
0

I'm going to be making an assumption here, but since the function is called checkPeople i'm going to assume you're doing the same check for every person (for instance: check for every person if he... exists?)

If that is the case:

function checkPeople()
{
    var people = Array.prototype.slice.call(arguments);
    return people.map(function(person) {
        return person == 'eric'; // only eric is a person today
    });
}

Now, you can do this:

var results = checkPerson('eric','peter','james'); // [true, false, false];
var result = checkPerson('eric'); // [true]
var result = checkPerson('james'); // [false]

If there are actually different checks for a first, second, and third parameter, then you indeed still have to implement the if (second) { .. } checks.

DoXicK
  • 4,784
  • 24
  • 22
  • Your first option under "now, you can do this" does not match your code. Did you mean to put the three names into an array and pass just the array? – jfriend00 Jul 25 '16 at 09:25
  • @jfriend00 crap, haha. i rewrote the function but forgot to update one line. Updated! – DoXicK Jul 25 '16 at 09:33