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

Mixed single and double quotes

Expand Messages
  • benquarmby
    While I m not sure anyone would consider mixing single and double quotes to be an error (or look like an error, or be more error prone), mixing them does
    Message 1 of 6 , Jul 1 3:31 AM
    • 0 Attachment
      While I'm not sure anyone would consider mixing single and double quotes to be an error (or look like an error, or be more error prone), mixing them does reduce maintainability in my experience.

      To that end, I'd love to see a warning for mixed double and single quotes in a similar way to mixed spaces and tabs but across a whole code file. I'm not sure I'd go so far as to enforce one over the other, although it might be a nice option to choose one in a similar way to choosing maximum line length.

      P.S. My motivation here is to keep JavaScript consistent across the offices I work with and take opinion out of the code. We'd rather rely on a cold, emotionless code quality tool than have format wars and debate about tabs vs. spaces.
    • douglascrockford
      ... My advice is to use double quotes for strings that are meaningful externally, like urls, html templates, and error messages; and to use single quotes
      Message 2 of 6 , Jul 1 6:01 AM
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "benquarmby" <ben.quarmby@...> wrote:

        > While I'm not sure anyone would consider mixing single and double quotes to be an error (or look like an error, or be more error prone), mixing them does reduce maintainability in my experience.

        > To that end, I'd love to see a warning for mixed double and single quotes in a similar way to mixed spaces and tabs but across a whole code file. I'm not sure I'd go so far as to enforce one over the other, although it might be a nice option to choose one in a similar way to choosing maximum line length.

        > P.S. My motivation here is to keep JavaScript consistent across the offices I work with and take opinion out of the code. We'd rather rely on a cold, emotionless code quality tool than have format wars and debate about tabs vs. spaces.


        My advice is to use "double quotes" for strings that are meaningful externally, like urls, html templates, and error messages; and to use 'single quotes' for strings that are meaningful internally, like character constants, property names, and symbols.

        I have not discover an automatic way to verify usage.
      • John Hawkinson
        When you deal with strings that contain double quotes and strings that contain single quotes: Warning: can t find token. Invalid input: cheese it is
        Message 3 of 6 , Jul 1 8:05 AM
        • 0 Attachment
          When you deal with strings that contain double quotes and strings
          that contain single quotes:
          "Warning: can't find token."
          'Invalid input: "cheese"'
          it is convenient to switch between single and double quotes.
          A rule that prevented that would be annoying.

          --jhawk@...
          John Hawkinson
        • douglascrockford
          ... You should be glad to know that what you find annoying is not a consideration. I am concerned instead with correctness and readability.
          Message 4 of 6 , Jul 1 9:48 AM
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, John Hawkinson <jhawk@...> wrote:

            > When you deal with strings that contain double quotes and strings
            > that contain single quotes:
            > "Warning: can't find token."
            > 'Invalid input: "cheese"'
            > it is convenient to switch between single and double quotes.
            > A rule that prevented that would be annoying.


            You should be glad to know that what you find annoying is not a consideration. I am concerned instead with correctness and readability.
          • sandyhead25
            ... Mixing of quotes can be a maintainability issue. In my own application I have all of my logic divided into libraries that are directly supplied to a
            Message 5 of 6 , Jul 2 10:00 AM
            • 0 Attachment
              > You should be glad to know that what you find annoying is not a consideration. I am concerned instead with correctness and readability.

              Mixing of quotes can be a maintainability issue. In my own application I have all of my logic divided into libraries that are directly supplied to a parent container function representing the entirety of the application. Many of these library components I have forked from other authors. Some have used single quotes and other used double quotes. If the logic in each library never moves out of the library you are fine, but if you want to transplant one condition to another library item you could run into trouble.

              With regards to Hawkinson's comment about nested quotes I come up with a tedious solution in my own app. I have chosen to use double quotes for everything where a quote is required in the JS syntax. If there exists a string already containing a double quote character as part of the string literal I escape it instead of shying away from consistency.

              var example = "what I am doing with \" characters";

              As long as you are using an editor that performs syntax coloring there is no harm to readability. Otherwise you actually have to read that there is a forward slash before the quote character and the string continues. I have not found this to be a problem

              Austin
            • benquarmby
              That sounds like good advice. Like you say though, difficult to verify. Would you analyze the contents of the string, or the subject of the assignment?
              Message 6 of 6 , Jul 3 2:34 PM
              • 0 Attachment
                That sounds like good advice. Like you say though, difficult to verify. Would you analyze the contents of the string, or the subject of the assignment? Something else?

                Without a reliable way to verify the intention of a string, applying the same advice you give for tabs vs. spaces seems preferable - Pick one and use it consistently.

                That advice would also prevent cowboy developers from creatively misinterpreting the difference between internal and external.

                --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
                >
                > My advice is to use "double quotes" for strings that are meaningful externally, like urls, html templates, and error messages; and to use 'single quotes' for strings that are meaningful internally, like character constants, property names, and symbols.
                >
                > I have not discover an automatic way to verify usage.
                >
              Your message has been successfully submitted and would be delivered to recipients shortly.