Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] Function return with and without value does not give any warning

Expand Messages
  • Satyam
    DOM Event listeners can return false to prevent them from doing their default action so it is not just a JavaScript issue but one promoted by the environment
    Message 1 of 7 , Mar 7 9:02 AM
    • 0 Attachment
      DOM Event listeners can return false to prevent them from doing their
      default action so it is not just a JavaScript issue but one promoted by
      the environment JavaScript is most likely to be found. Of course you
      could return something in both cases, but the issue is that most
      documentation, tutorials and so on promote this practice, return false
      or don't return anything, and I don't think it would be practical to
      swim against the current. And I'm certainly not saying that it is a
      good practice, but it is there, documented and promoted, like it or not.

      Satyam

      El 07/03/2010 12:53, Woomla escribió:
      >
      > If a function does not return anything than it is ok to use return
      > without a value, although I don't consider this as 'best practise'.
      >
      > In my example, however, I have both return with value and return
      > without value. In a more strict language as C/C++ this is forbidden.
      > Unfortunately it is allowed in Javascript, which, in my opinion, could
      > lead to bugs that are hard to track.
      >
      > If you check the results of a function that did not return a value,
      > the result is undefined or 'undefined', or another value assigned to
      > undefined. Causing enough confusement, and hard to track down why
      > twenty calls later some value is undefined.
      >
      > I think you should program your functions to always return a value, or
      > never. And don't mix this. That way, you're always forced to think
      > about and define what you're functions should do an return. Even
      > better, write a unittest where you define the input and outcome of
      > your function.
      >
      > Anyway, a little jslint warning, which I personally would threat as an
      > error, would be very helpfull here.
      >
      > --- In jslint_com@yahoogroups.com
      > <mailto:jslint_com%40yahoogroups.com>, Mark Volkmann
      > <r.mark.volkmann@...> wrote:
      > >
      > > It could be a function that doesn't return anything and return is
      > being used
      > > just to exit the function early. In that case using return without a
      > value
      > > makes sense.
      > >
      > > On Fri, Mar 5, 2010 at 8:24 AM, Woomla <woomla@...> wrote:
      > >
      > > >
      > > >
      > > > The code snippet below doesn't give any errors or warnings. But
      > return with
      > > > and without a value is used. Shouldn't that at least issue a warning?
      > > >
      > > > "use strict";
      > > >
      > > > function test(a, b)
      > > > {
      > > > if (a)
      > > > {
      > > > return true;
      > > > }
      > > > if (b)
      > > > {
      > > > return;
      > > > }
      > > > }
      > > >
      > >
      > > --
      > > R. Mark Volkmann
      > > Object Computing, Inc.
      > >
      > >
      > > [Non-text portions of this message have been removed]
      > >
      >
      >
      >
      >
      >
      > No virus found in this incoming message.
      > Checked by AVG - www.avg.com
      > Version: 9.0.733 / Virus Database: 271.1.1/2727 - Release Date: 03/06/10 20:34:00
      >
      >


      [Non-text portions of this message have been removed]
    • Woomla
      ... Unfortunately I must agree. And I surely don t want to swim againts the current. If you can circumvent the jslint warning by adding return undefined, then
      Message 2 of 7 , Mar 8 2:16 AM
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Stefan Weiss <weiss@...> wrote:
        >
        > > I think you should program your functions to always return a value,
        > > or never. And don't mix this. That way, you're always forced to think about
        > > and define what you're functions should do an return. Even better, write
        > > a unittest where you define the input and outcome of your function.
        > >
        > > Anyway, a little jslint warning, which I personally would threat as
        > > an error, would be very helpfull here.
        >
        > I don't agree. This would be similar to the "strict warnings" setting in
        > Firefox, which does as you suggest, and is so annoying that everybody I
        > know leaves them turned off. We did have a phase where we tried to
        > conform to the "always (or never) explicitly return a value" idea, but
        > adding "return undefined" at the end of functions quickly became a
        > nuisance. It didn't add any kind of safety, in my experience, because
        > the behavior was exactly the same as before.

        Unfortunately I must agree. And I surely don't want to swim againts the current. If you can circumvent the jslint warning by adding return undefined, then probably it's not a good idea to have the warning in the first place. But if there was a warning, it would still mean that the programmer has to pay attention to his or her function and it can not slip through unforseen. All my functions either have or don't have return values, and a mix for me will indicate an error.

        If only the rest of the world would also program that strict :-) But then it is probably better to make it a typed language. (Which in fact phplint is doing!)


        > If you feel you really need this feature, you could just run your code
        > in Firefox, and turn the strict JavaScript warnings on.

        Of course I can do this. And probably I can do this to get all kind of bad programming and bugs visible. But it is so much easier to use jslint. And also all warnings from third party scripts appear as well. And there will be many, so it will be hard to spot my single
        warning.
      • Douglas Crockford
        ... C s interest in producing an error here is due to its type system. It want to generate different code for void functions than for other types of functions.
        Message 3 of 7 , Mar 8 11:46 AM
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Woomla" <woomla@...> wrote:
          >
          > The code snippet below doesn't give any errors or warnings. But return with and without a value is used. Shouldn't that at least issue a warning?
          >
          >
          > "use strict";
          >
          > function test(a, b)
          > {
          > if (a)
          > {
          > return true;
          > }
          > if (b)
          > {
          > return;
          > }
          > }
          >

          C's interest in producing an error here is due to its type system. It want to generate different code for void functions than for other types of functions. JavaScript does not have this difficulty. There are no void functions. Every function returns a value, even if it is only the undefined value.

          I am reluctant to add more complaints to JSLint unless there is some evidence that doing so will make programs better. I don't see the benefit to the warning you are demanding. Can you offer some evidence?
        Your message has been successfully submitted and would be delivered to recipients shortly.