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

SV: [jslint] Re: Indentation quirk?

Expand Messages
  • caerusx
    Hi I rewrote all my functions declared as var to functions declared as function. (Thanks for giving me an insight that it s safe to declare nested functions).
    Message 1 of 7 , Jan 23, 2012
    • 0 Attachment
      Hi

      I rewrote all my functions declared as var to functions declared as
      function. (Thanks for giving me an insight that it's safe to declare
      nested functions). And I hit another problem: JSLint does not like
      'this' keyword inside function, it fails to validate function with an
      error "Strict violation".

      But function doesn't even supposed to access global 'this'
      object. It's just harmless jQuery callback which expects to be called
      in a context of DOM node.

      --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@...> wrote:
      >
      > I don't see this going anywhere, but I'd like to point out that
      >
      > 1) A function call split over 3-4 lines is not a "one line function". If you would like to make it one, stop declaring new functions inside the call.
      > 2) Why would you use "var" to first declare and then define a function. JavaScript has "function" for this purpose. Jslint does not complain about function statements separated from var statements.
      >
      > /Jakob
      >
      >
      > Fra: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] På vegne af caerusx
      > Sendt: 23. januar 2012 13:36
      > Til: jslint_com@yahoogroups.com
      > Emne: SV: [jslint] Re: Indentation quirk?
      >
      >
      >
      >
      > Hi
      >
      > Very often one line function does not fit in the same line with
      > function keyword, so it's naturally to write it as open block.
      >
      > Consider:
      >
      > some_function_with_the_long_name(arg1, arg2, arg3, arg4,
      > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~function () {
      > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~other_some_long_function(par1, par2);
      > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~});
      >
      > You just can't write lambda function in compact form, it does not
      > fit. Rewriting code with named function, is less readable:
      >
      > var binder3;
      >
      > [... some lines of code ...]
      >
      > binder3 = function () {
      > ~~other_some_long_function(par1, par2);
      > };
      >
      > some_function_with_the_long_name(arg1, arg2, arg3, arg4, binder3);
      >
      > You have too look back, what binder3 is actually doing. Though it just
      > a three lines upper, but it definitely takes more time to look back.
      >
      > More over, you have to split 'binder3' declaration and assignment. If
      > you assign it in declaration statement, code become even more
      > unreadable (you have to look back more code to find binder3
      > definition).
      >
      > And by JSLint rules, you can't declare var just before calling
      > 'some_function_with_the_long_name', as you get an error: "Combine this
      > with the previous 'var' statement.".
      >
      > You may object, that you may give a better name to 'binder3'. But any
      > good name is not as expressive as code. You may give a name like
      > 'compare_tuples', but it does not express the all meaning (what
      > relation is used for comparison, and what parts of tuple actually
      > compared).
      >
      > --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@> wrote:
      > >
      > > Hi
      > >
      > > The formatting problem you outlined had to do with how your code should be distributed over multiple lines, so it is not "just one line". If it was just one line jslints line breaking rules would hardly apply.
      > >
      > > I recommend that you improve the readability of your code because that makes it better code. That it also then happens to comply with the recommendations of jslint is an added bonus.
      > >
      > > /Jakob
      > >
      > > Fra: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] På vegne af caerusx
      > > Sendt: 23. januar 2012 12:38
      > > Til: jslint_com@yahoogroups.com
      > > Emne: SV: [jslint] Re: Indentation quirk?
      > >
      > >
      > >
      > >
      > > Hi
      > >
      > > For one liners it's better to use lambda forms. No matter how
      > > descriptive name is, better to see an actual code immediately, if it
      > > just one line.
      > >
      > > Often lambda forms are used just to bind some parameters to existing
      > > functions, or just to pass some expression (e.g. sort function), or
      > > some simple filtering function. And it's weird to give an explicit
      > > name to these one liners in language supporting lambda forms.
      > >
      > > It's just my opinion.
      > >
      > > Nevertheless, I don't think that it is a good idea to rewrite a
      > > readable code to less readable just to pass a lint.
      > >
      > > Like in my other topic with cyclic dependent functions. It is possible
      > > to work around lint requirement by splitting declarations of variable
      > > and assignment to separate statements, but why to do this if it
      > > actually complicates code.
      > >
      > > --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@> wrote:
      > > >
      > > > Good morning,
      > > >
      > > > Your problem seems to be your insistence on declaring a function inside a function call. This is much better (from a readability perspective):
      > > >
      > > > function myfunc() { � }
      > > > func(1, 2, 3, 4, 5, 6, myfunc);
      > > >
      > > > Regards,
      > > > Jakob
      > > >
      > > >
      > > > Fra: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] P� vegne af caerusx
      > > > Sendt: 23. januar 2012 09:57
      > > > Til: jslint_com@yahoogroups.com
      > > > Emne: [jslint] Re: Indentation quirk?
      > > >
      > > > Good morning!
      > > >
      > > > I was thinking about mixing open and compact forms. And I think
      > > > disallowing to use open inside compact and vice verse brings
      > > > inconveniences.
      > > >
      > > > It's fine for lint to use function parameters in compact form:
      > > >
      > > > func(1, 2, 3,
      > > > ~~~~~4, 5, 6, function () {});
      > > >
      > > > But when one parameter listed in compact form internally contains open
      > > > form, lint fails to validate:
      > > >
      > > > func(1, 2, 3,
      > > > ~~~~~4, 5, 6, function () {
      > > > ~~~~~});
      > > >
      > > > To pass validation, it's needed to rewrite outer block to open
      > > > form. Do you agree that code below, which is correct from lint point
      > > > of view, looks worse for a human?
      > > >
      > > > func(
      > > > ~~1,
      > > > ~~2,
      > > > ~~3,
      > > > ~~4,
      > > > ~~5,
      > > > ~~6,
      > > > ~~function () {
      > > > ~~}
      > > > );
      > > >
      > > > [Non-text portions of this message have been removed]
      > > >
      > >
      > >
      > > [Non-text portions of this message have been removed]
      > >
      >
      >
      > [Non-text portions of this message have been removed]
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.