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

option wish for single line if

Expand Messages
  • Mark Volkmann
    I wish JSLint had an option that would allow code like this: if (!someVariable) return; I understand the danger of allowing code without braces like: if
    Message 1 of 14 , Feb 22, 2010
    • 0 Attachment
      I wish JSLint had an option that would allow code like this:

      if (!someVariable) return;

      I understand the danger of allowing code without braces like:

      if (condition)
      then-line
      else
      else-line

      However, it bugs me to be forced to use three lines for simple ifs.

      How about a "onelineif" option that is off by default, but when on
      allows if statements without braces on if it fits on one line?

      --
      R. Mark Volkmann
      Object Computing, Inc.
    • Douglas Crockford
      ... if (!someVariable) {return;}
      Message 2 of 14 , Feb 22, 2010
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
        >
        > I wish JSLint had an option that would allow code like this:
        >
        > if (!someVariable) return;

        > However, it bugs me to be forced to use three lines for simple ifs.
        >
        > How about a "onelineif" option that is off by default, but when on
        > allows if statements without braces on if it fits on one line?

        if (!someVariable) {return;}
      • Mark Volkmann
        ... Ah ... it never occurred to me that JSLint might approve of that. However, I can t get that to work. For me JSLint says Expected return to have an
        Message 3 of 14 , Feb 22, 2010
        • 0 Attachment
          On Mon, Feb 22, 2010 at 5:33 PM, Douglas Crockford <douglas@...>wrote:

          > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
          > Volkmann <r.mark.volkmann@...> wrote:
          > >
          > > I wish JSLint had an option that would allow code like this:
          > >
          > > if (!someVariable) return;
          >
          > > However, it bugs me to be forced to use three lines for simple ifs.
          > >
          > > How about a "onelineif" option that is off by default, but when on
          > > allows if statements without braces on if it fits on one line?
          >
          > if (!someVariable) {return;}
          >

          Ah ... it never occurred to me that JSLint might approve of that. However, I
          can't get that to work. For me JSLint says "Expected 'return' to have an
          indentation ..." and "Expected '}' to have an indentation ...". I think I
          have the newest version of JSLint. Do I need to set an option to enable
          that? I don't see one at the website.

          --
          R. Mark Volkmann
          Object Computing, Inc.


          [Non-text portions of this message have been removed]
        • Douglas Crockford
          ... Turn off the Strict white space option.
          Message 4 of 14 , Feb 22, 2010
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
            >
            > On Mon, Feb 22, 2010 at 5:33 PM, Douglas Crockford <douglas@...>wrote:
            >
            > > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
            > > Volkmann <r.mark.volkmann@> wrote:
            > > >
            > > > I wish JSLint had an option that would allow code like this:
            > > >
            > > > if (!someVariable) return;
            > >
            > > > However, it bugs me to be forced to use three lines for simple ifs.
            > > >
            > > > How about a "onelineif" option that is off by default, but when on
            > > > allows if statements without braces on if it fits on one line?
            > >
            > > if (!someVariable) {return;}
            > >
            >
            > Ah ... it never occurred to me that JSLint might approve of that. However, I
            > can't get that to work. For me JSLint says "Expected 'return' to have an
            > indentation ..." and "Expected '}' to have an indentation ...". I think I
            > have the newest version of JSLint. Do I need to set an option to enable
            > that? I don't see one at the website.


            Turn off the Strict white space option.
          • Mark Volkmann
            ... Thanks! I see that that works. I wish there was a middle ground though. Even though I want to be able to write if (!someVariable) { return; } , I still
            Message 5 of 14 , Feb 22, 2010
            • 0 Attachment
              On Mon, Feb 22, 2010 at 6:39 PM, Douglas Crockford <douglas@...>wrote:

              > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
              > Volkmann <r.mark.volkmann@...> wrote:
              > >
              > > On Mon, Feb 22, 2010 at 5:33 PM, Douglas Crockford <douglas@...>wrote:
              > >
              > > > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com><jslint_com%
              > 40yahoogroups.com>, Mark
              >
              > > > Volkmann <r.mark.volkmann@> wrote:
              > > > >
              > > > > I wish JSLint had an option that would allow code like this:
              > > > >
              > > > > if (!someVariable) return;
              > > >
              > > > > However, it bugs me to be forced to use three lines for simple ifs.
              > > > >
              > > > > How about a "onelineif" option that is off by default, but when on
              > > > > allows if statements without braces on if it fits on one line?
              > > >
              > > > if (!someVariable) {return;}
              > > >
              > >
              > > Ah ... it never occurred to me that JSLint might approve of that.
              > However, I
              > > can't get that to work. For me JSLint says "Expected 'return' to have an
              > > indentation ..." and "Expected '}' to have an indentation ...". I think I
              > > have the newest version of JSLint. Do I need to set an option to enable
              > > that? I don't see one at the website.
              >
              > Turn off the Strict white space option.
              >

              Thanks! I see that that works. I wish there was a middle ground though. Even
              though I want to be able to write "if (!someVariable) { return; }", I still
              care that my code is indented properly and don't want to disable that
              checking.

              --
              R. Mark Volkmann
              Object Computing, Inc.


              [Non-text portions of this message have been removed]
            • Jordan
              Alternatively, you could opt for readability, and only use a single return statement per function, as the last line in the function. Thus, rather than: if
              Message 6 of 14 , Feb 22, 2010
              • 0 Attachment
                Alternatively, you could opt for readability, and only use a single return statement per function, as the last line in the function. Thus, rather than:
                if (!variable) { return; }
                // more code

                You would have:
                var returnValue;
                if (variable) {
                // more code
                // set returnValue to something if you want it returned
                }
                return returnValue;

                --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
                >
                > On Mon, Feb 22, 2010 at 6:39 PM, Douglas Crockford <douglas@...>wrote:
                >
                > > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Mark
                > > Volkmann <r.mark.volkmann@> wrote:
                > > >
                > > > On Mon, Feb 22, 2010 at 5:33 PM, Douglas Crockford <douglas@>wrote:
                > > >
                > > > > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com><jslint_com%
                > > 40yahoogroups.com>, Mark
                > >
                > > > > Volkmann <r.mark.volkmann@> wrote:
                > > > > >
                > > > > > I wish JSLint had an option that would allow code like this:
                > > > > >
                > > > > > if (!someVariable) return;
                > > > >
                > > > > > However, it bugs me to be forced to use three lines for simple ifs.
                > > > > >
                > > > > > How about a "onelineif" option that is off by default, but when on
                > > > > > allows if statements without braces on if it fits on one line?
                > > > >
                > > > > if (!someVariable) {return;}
                > > > >
                > > >
                > > > Ah ... it never occurred to me that JSLint might approve of that.
                > > However, I
                > > > can't get that to work. For me JSLint says "Expected 'return' to have an
                > > > indentation ..." and "Expected '}' to have an indentation ...". I think I
                > > > have the newest version of JSLint. Do I need to set an option to enable
                > > > that? I don't see one at the website.
                > >
                > > Turn off the Strict white space option.
                > >
                >
                > Thanks! I see that that works. I wish there was a middle ground though. Even
                > though I want to be able to write "if (!someVariable) { return; }", I still
                > care that my code is indented properly and don't want to disable that
                > checking.
                >
                > --
                > R. Mark Volkmann
                > Object Computing, Inc.
                >
                >
                > [Non-text portions of this message have been removed]
                >
              • Cheney, Edward A SSG RES USAR USARC
                ... There is no middle ground. Either beautify your code in conformance to a rule set or don t. It may help if you use an automated beautifier that conforms
                Message 7 of 14 , Feb 22, 2010
                • 0 Attachment
                  > Thanks! I see that that works. I wish there was a middle ground though. Even
                  > though I want to be able to write "if (!someVariable) { return; }", I still
                  > care that my code is indented properly and don't want to disable that
                  > checking.

                  There is no middle ground. Either beautify your code in conformance to a rule set or don't. It may help if you use an automated beautifier that conforms to that rule set.

                  Additionally, I have found that testing for a variable name is less reliable than testing for a variable value unless your objective is to determine if that namespace exists within a given inheritable scope. I would recommend setting all your variable names at the top of your function and then test for undefined.

                  if (varName === undefined) {return;}

                  In that example there is no confusion. A variable can be set else where and inherited into a grandchild function of execution, which then will trigger a decision contrary to intentions of some localized invocation, which is confusing and produces an unexpected decision.

                  Austin
                • Jordan
                  Austin, since undefined can be overwritten, your test isn t reliable either. What you SHOULD do in your case is: if (typeof(varName) === undefined ) { }
                  Message 8 of 14 , Feb 22, 2010
                  • 0 Attachment
                    Austin, since "undefined" can be overwritten, your test isn't reliable either.

                    What you SHOULD do in your case is:
                    if (typeof(varName) === "undefined") { }

                    Also see my other comment about having a single exit point from a function.

                    --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
                    >
                    >
                    > > Thanks! I see that that works. I wish there was a middle ground though. Even
                    > > though I want to be able to write "if (!someVariable) { return; }", I still
                    > > care that my code is indented properly and don't want to disable that
                    > > checking.
                    >
                    > There is no middle ground. Either beautify your code in conformance to a rule set or don't. It may help if you use an automated beautifier that conforms to that rule set.
                    >
                    > Additionally, I have found that testing for a variable name is less reliable than testing for a variable value unless your objective is to determine if that namespace exists within a given inheritable scope. I would recommend setting all your variable names at the top of your function and then test for undefined.
                    >
                    > if (varName === undefined) {return;}
                    >
                    > In that example there is no confusion. A variable can be set else where and inherited into a grandchild function of execution, which then will trigger a decision contrary to intentions of some localized invocation, which is confusing and produces an unexpected decision.
                    >
                    > Austin
                    >
                  • Cheney, Edward A SSG RES USAR USARC
                    Jordan, ... That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions
                    Message 9 of 14 , Feb 23, 2010
                    • 0 Attachment
                      Jordan,

                      > Austin, since "undefined" can be overwritten, your test isn't reliable either.

                      That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:

                      var parent = function () {
                      var test = "stringValue",
                      child = function () {
                      var test;
                      if (test === undefined) {
                      alert("true");
                      }
                      };
                      };

                      That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.

                      Austin
                    • Jordan
                      You misundersand. undefined itself can be redefined. Simply running var undefined = 1; will break your entire code. NEVER rely on the variable undefined .
                      Message 10 of 14 , Feb 23, 2010
                      • 0 Attachment
                        You misundersand.

                        "undefined" itself can be redefined.
                        Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".

                        --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
                        >
                        > Jordan,
                        >
                        > > Austin, since "undefined" can be overwritten, your test isn't reliable either.
                        >
                        > That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:
                        >
                        > var parent = function () {
                        > var test = "stringValue",
                        > child = function () {
                        > var test;
                        > if (test === undefined) {
                        > alert("true");
                        > }
                        > };
                        > };
                        >
                        > That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.
                        >
                        > Austin
                        >
                      • Michael Lorton
                        Are you kidding? It s lamentable that undefined and null aren t constants but there s no sense programming around it. Never type in var undefined = 1;
                        Message 11 of 14 , Feb 23, 2010
                        • 0 Attachment
                          Are you kidding? It's lamentable that "undefined" and "null" aren't constants but there's no sense programming around it. Never type in "var undefined = 1;" and never use any library that does.

                          M.



                          Sent from my iPhone

                          On Feb 23, 2010, at 10:20 PM, "Jordan" <ljharb@...> wrote:

                          You misundersand.

                          "undefined" itself can be redefined.
                          Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".

                          --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:

                          Jordan,

                          Austin, since "undefined" can be overwritten, your test isn't reliable either.

                          That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:

                          var parent = function () {
                          var test = "stringValue",
                          child = function () {
                          var test;
                          if (test === undefined) {
                          alert("true");
                          }
                          };
                          };

                          That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.

                          Austin





                          ------------------------------------

                          Yahoo! Groups Links
                        • Jordan
                          There s no sense programming around flaws in the language? It is incredibly irresponsible to just decide I don t like that, so I m going to act like it s not
                          Message 12 of 14 , Feb 23, 2010
                          • 0 Attachment
                            There's no sense programming around flaws in the language?

                            It is incredibly irresponsible to just decide "I don't like that, so I'm going to act like it's not a problem."

                            Obviously you wouldn't type that into your code. Obviously you wouldn't intentionally divide by zero either, but you would still program defensively to prevent DivideByZero errors.

                            JSLint is about programming defensively and properly, not about "doing what works".

                            --- In jslint_com@yahoogroups.com, Michael Lorton <mlorton@...> wrote:
                            >
                            > Are you kidding? It's lamentable that "undefined" and "null" aren't constants but there's no sense programming around it. Never type in "var undefined = 1;" and never use any library that does.
                            >
                            > M.
                            >
                            >
                            >
                            > Sent from my iPhone
                            >
                            > On Feb 23, 2010, at 10:20 PM, "Jordan" <ljharb@...> wrote:
                            >
                            > You misundersand.
                            >
                            > "undefined" itself can be redefined.
                            > Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".
                            >
                            > --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@> wrote:
                            >
                            > Jordan,
                            >
                            > Austin, since "undefined" can be overwritten, your test isn't reliable either.
                            >
                            > That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:
                            >
                            > var parent = function () {
                            > var test = "stringValue",
                            > child = function () {
                            > var test;
                            > if (test === undefined) {
                            > alert("true");
                            > }
                            > };
                            > };
                            >
                            > That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.
                            >
                            > Austin
                            >
                            >
                            >
                            >
                            >
                            > ------------------------------------
                            >
                            > Yahoo! Groups Links
                            >
                          • Cheney, Edward A SSG RES USAR USARC
                            If you have a variable named undefined your code deserves to break. Austin
                            Message 13 of 14 , Feb 23, 2010
                            • 0 Attachment
                              If you have a variable named "undefined" your code deserves to break.

                              Austin
                            • Michael Lorton
                              That well is bottomless. If a library is deliberately sabotaging your code -- and sabotage is the kindest interpretation of var undefined = 1; -- no amount
                              Message 14 of 14 , Feb 24, 2010
                              • 0 Attachment
                                That well is bottomless.

                                If a library is deliberately sabotaging your code -- and sabotage is the kindest interpretation of "var undefined = 1;" -- no amount of defensive programming is going to save you.

                                And no, I wouldn't program defensively to prevent DivideByZero errors necessarily. Say for example I was writing a program to compute the average of the list passed in as an argument. A naive implementation would throw DivideByZero if given an empty list. What would a "defensive" implementation do?

                                Well, nothing. There is nothing to be done. You can't return 0, that's the wrong answer. You could return NaN, but that won't really help anything: either the calling program will test for it (couldn't it just have tested for a zero-length list?) or some other error will be thrown at some point in the future.

                                My point, and I do have one, is that it only makes sense to program defensively against problems you can actually solve. There's no way to calculate the average of a zero-length list, and there's no way for a Javascript program to survive in the face of a suicidal library.

                                M.





                                ________________________________
                                From: Jordan <ljharb@...>
                                To: jslint_com@yahoogroups.com
                                Sent: Tue, February 23, 2010 10:39:44 PM
                                Subject: [jslint] Re: option wish for single line if

                                There's no sense programming around flaws in the language?

                                It is incredibly irresponsible to just decide "I don't like that, so I'm going to act like it's not a problem."

                                Obviously you wouldn't type that into your code. Obviously you wouldn't intentionally divide by zero either, but you would still program defensively to prevent DivideByZero errors.

                                JSLint is about programming defensively and properly, not about "doing what works".

                                --- In jslint_com@yahoogroups.com, Michael Lorton <mlorton@...> wrote:
                                >
                                > Are you kidding? It's lamentable that "undefined" and "null" aren't constants but there's no sense programming around it. Never type in "var undefined = 1;" and never use any library that does.
                                >
                                > M.
                                >
                                >
                                >
                                > Sent from my iPhone
                                >
                                > On Feb 23, 2010, at 10:20 PM, "Jordan" <ljharb@...> wrote:
                                >
                                > You misundersand.
                                >
                                > "undefined" itself can be redefined.
                                > Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".
                                >
                                > --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@> wrote:
                                >
                                > Jordan,
                                >
                                > Austin, since "undefined" can be overwritten, your test isn't reliable either.
                                >
                                > That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:
                                >
                                > var parent = function () {
                                > var test = "stringValue",
                                > child = function () {
                                > var test;
                                > if (test === undefined) {
                                > alert("true");
                                > }
                                > };
                                > };
                                >
                                > That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.
                                >
                                > Austin
                                >
                                >
                                >
                                >
                                >
                                > ------------------------------------
                                >
                                > Yahoo! Groups Links
                                >




                                ------------------------------------

                                Yahoo! Groups Links



                                [Non-text portions of this message have been removed]
                              Your message has been successfully submitted and would be delivered to recipients shortly.