1
  next() {
    if (this.form.valid) {
      this.fooService.login(this.form.get('email').value, this.form.get('password').value)
      .subscribe(
        principal => {
          this.barService.put('sessionId', principal.token);
          this.doStuffWithToken(user.token);
        },
        error => console.log(error)
      );
      this.goToNextStep();
    }
    return false; //What? 
  }

What's the point of the return false line here? It's done all over the code I am working with, and I don't understand why.

VSO
  • 11,546
  • 25
  • 99
  • 187
  • How is `next` called…? Would the caller have any use for this value? – deceze Oct 31 '16 at 15:27
  • Depends on what is calling the function and what it expects as return and what it does depending on the return value. – Günter Zöchbauer Oct 31 '16 at 15:27
  • Where does that code comes from? the return type of your method is not specified, so either it's supposed to return a boolean, or it's supposed to return void and the "return false" part is just... a wtf return. – Supamiu Oct 31 '16 at 15:28
  • It's returning false if the form isn't valid. What is tripping you up about it? – Carcigenicate Oct 31 '16 at 15:28
  • 2
    @Carcigenicate not true. It is returning false all the time. – Adjit Oct 31 '16 at 15:29
  • @everyone: This function is not used to return a value. It's meant to execute some code. I feel like there is zero point to 'return false' here. – VSO Oct 31 '16 at 15:30
  • This question can only be answered through speculation, QED. Voting to close. – deceze Oct 31 '16 at 15:35
  • @deceze: I can't really give more info - it's used at the end of EVERY function that is used to execute other actions. I wanted to see if I was missing something obvious - looks like I am not. – VSO Oct 31 '16 at 15:37
  • If you cannot find any place where the return value is actually being used… well, then it's really as nonsensical as it looks. – deceze Oct 31 '16 at 15:40
  • 1
    Look at this maybe: http://stackoverflow.com/questions/19166296/javascript-return-true-or-return-false-when-and-how-to-use-it – thekodester Oct 31 '16 at 16:45
  • @Adjit Whoops, you're right. I read it wrong – Carcigenicate Oct 31 '16 at 16:50

3 Answers3

5

If you don't return false, your function will return undefined instead. Whether this may or may not be an issue depends on the code that calls the function.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • 1
    What would be the benefit to this, if it is always returning the same thing? whether it be `false` or `undefined`? – Adjit Oct 31 '16 at 15:32
  • 1
    @Adjit depends on how that value is being consumed. It might be that whatever is calling `next()` might expect a boolean but doesn't really matter what the boolean is. – VLAZ Oct 31 '16 at 15:32
  • @vlaz That would be pretty odd… a function making an explicit difference between `undefined` and `false` and then… doing… what? Throwing an exception? "Return value not boolean"? Seems just as pointless. – deceze Oct 31 '16 at 15:34
  • 1
    @deceze Oh, I don't mean to imply that it makes _sense_ to have that, I'm just saying you could have that. There are a lot of things that don't make complete sense in either third party APIs or even your own code base. Perhaps it served some purpose once but its scope has been widened or perhaps it was badly designed at first. \*shrug\* whatever the case, it's _possible_ that a boolean is required. Still, if what *Kamal* is saying is true, `false` might even be the required output. – VLAZ Oct 31 '16 at 15:38
1

I think the next() is a middleware function, without the return false, its gonna wait for an answer forever. And if the form is valid goToNextStep() will be called.

Kamal
  • 160
  • 8
  • Can you elaborate on what you mean? – VSO Oct 31 '16 at 15:33
  • this function check the user input first before giving the data to the goToNextStep(). The next() is like a firewall you only gonna pass if the data is valid, else gonna wait for something, return false gonna prevent the waiting for something. With the return false, you should show some error message. – Kamal Oct 31 '16 at 15:37
  • @Kamal but even after the `goToNextStep()` is completed, lets say you showed some error message on a false return, it will always end up showing that error message. The only thing I could think of if `goToNextStep()` changes the "error" message and the false return tells the page to show said "error" message, whether or not it has been altered by the `goToNextStep()` but again, as someone else pointed out this is all speculation. – Adjit Oct 31 '16 at 17:40
0

This way you can check is form valid. For example:

if (next() === false) {
    alert('Form is invalid');
}
Eugene Tsakh
  • 2,777
  • 2
  • 14
  • 27