News for hackers. Stuff that matters.
Re:Show HN: Learn and practice modern JavaScript by arethuza on Thursday January 03, 2019 @10:09AM (#18815620)
I skimmed this (which looks good) and the Airbnb guide recommended (recommended in another comment) and I can't help noticing that one uses double quotes for strings and the other single quotes (Airbnb).

Is there actually any sensible argument one way or the other - that isn't just being consistent with your team/project/platform...?

Re:Show HN: Learn and practice modern JavaScript by khalilravanna on Thursday January 03, 2019 @10:44AM (#18815909)
For these sorts of style rules we've started using Prettier (https://prettier.io/) to auto-format code for style. We set VS Code to just run it every time you save a file. It's been a huge win for us so developers don't have to spend time on trivial stuff like double vs single quotes and the like. No more having to comment on style inconsistency in PRs, we can focus on actual code quality. And no more arguments about what's the right style rules.
Re:Show HN: Learn and practice modern JavaScript by ergothus on Thursday January 03, 2019 @11:36AM (#18816382)
Prettier doesn't resolve my primary issue with excessive linting rules.

The way I see it, there are three kinds of syntax linting rules:

1) Potential bugs. These are where a syntax will technically work, but makes it easy for an unintended bug to creep in. Most everyone agrees that these are bad. (There are a few arguments about what qualifies, but this is nonetheless a less-argued-about category.

2) Purely stylistic. This is where Prettier shines, because everyone wants the "more readable" option - where "readable" means "more familiar". Prettier allows you to write your preferred style, though of course anything you READ will be in the defined style (absent two-way conversion).

3) Expressiveness. This is where you're formatting your code to communicate. While Perl is notorious for this - the advocates love it because well-written code is easy to follow and maintain and the detractors hate it because just because you CAN write clearly in it, the skill to do so is rarely developed and the result is near-gibberish, even from skilled coders - like a story that has every sentence written by a different person. Java is the reverse - the advocates love it because the code tends to be very uniform and the detractors hate it because it's universally verbose and unclear.

That last category is why I'm leery of excessive linting. If I'm dealing with code that changes hands often, a Java style makes sense - newspapers written for a low but common reading level, even if the reader is capable of much more nuance. In this scenario nuance would risk being ignored, or worse, being mis-applied, which means the result would be misleading. If I'm dealing with code that is owned by a team that has routine staffing changes but no complete upsets, nuance has a real benefit - reading code is faster and more accurate/informative, with the result of fewer bugs (citation needed).

But saying "It's okay to use Prettier and stop arguing about linting" dismisses the nuanced case. It carries the assumption that linting rules - any rules - are automatically valuable regardless of the choices made, and if you don't like it than write what you like and just use Prettier.

Linting rules (and linting arguments) exist for a reason - these are LANGUAGES that we are using to communicate - and as much as I'm tired of the arguments too, until we honestly figure out how to communicate well, ignoring the arguments doesn't help.

Re:Show HN: Learn and practice modern JavaScript by khalilravanna on Thursday January 03, 2019 @01:57PM (#18818006)
I agree that nuance and discussion about style is incredibly important because, as you aptly point out, we're writing in a language used for communication. That being said, since we've introduced Prettier, I haven't encountered any changes it's made to code that are anything more than trivial formatting/purely-stylistic changes.

I think readability and expressiveness are more about larger scale patterns in code writing (writing self-documenting code with descriptive variable and function names, writing modular code, etc). These are things that should be continuously discussed and improved upon for clarity and alignment. The really granular formatting changes that Prettier chiefly concerns itself with don't seem to be a concern in this case. If you could point me at a more specific case where you think Prettier mucks things up I'd love to see more of what you're talking about.

Re:Show HN: Learn and practice modern JavaScript by ergothus on Thursday January 03, 2019 @03:17PM (#18818870)
I could be misunderstanding (I've not yet used Prettier), but I thought Prettier was agnostic to the linting rules used. I was arguing not that Prettier is bad, but the _excessive_ linting rules were bad (Prettier should be fine for reducing arguments over trivial rules that neither impact bugs nor expressiveness), and further, that arguing that excessive linting wasn't a problem because Prettier can let you ignore them is likewise bad.

None of which says that Prettier itself is bad, but that excessive linting rules, with or without Prettier, are bad.

If I'm wrong about what Prettier does, please let me know. If you're discussing a default set of linting rules Prettier uses, cool, but I can't really say if I think they impact expressiveness since I'm not familiar with that set of rules.

Re:Show HN: Learn and practice modern JavaScript by jacobwg on Thursday January 03, 2019 @04:05PM (#18819263)
I think you are misunderstanding what Prettier does - Prettier is more in line with gofmt as it is a formatter not a linter. Prettier parses the JS code into an abstract syntax tree and then outputs that AST back into code using a defined set of rules, so you end up with one unified style. Prettier intentionally doesn't give you many configuration options for this style, with the goal of eliminating the discussion about which style to use.

If you are using Prettier, you can use the Prettier config with eslint or tslint, which fully disables the style rules for those two linters and lets them focus solely on linting for errors and structural issues.

Re:Show HN: Learn and practice modern JavaScript by ergothus on Thursday January 03, 2019 @06:00PM (#18820212)
Ah, I thought it consumed the style rules and rewrote to them, so you could pick your preferred styles and have everyone submit code that met them even if they didn't write in that way. Which sounds mostly correct, except for the "pick" part.
Re:Show HN: Learn and practice modern JavaScript by tokyodude on Thursday January 03, 2019 @01:33PM (#18817703)
That whole issue is arguably old JavaScript. It was because HTML style preferred double quotes so using since quotes in JS made that easier.

Modern Javascript has template literals using backticks so arguably the new rule should be used double quote everywhere except use a template literals for HTML or whenever doing string manipulation.

The number one thing I hate about using single quotes it is matches almost no other languages so it ruins muscle memory if switching between languages.

JS itself prefers double quotes. See JSON.stringify or log anything to the console that gets auto quoted, always uses double quotes

Re:Show HN: Learn and practice modern JavaScript by kiriakasis on Saturday January 05, 2019 @07:45PM (#18835497)
The fact that JSON.stringify uses double quotes is slightly in favour of using single quotes in handwritten code
Re:Show HN: Learn and practice modern JavaScript by marcosdumay on Thursday January 03, 2019 @02:09PM (#18818151)
It matches SQL, that is the one language one is most likely to write at the same time as any other language.
Re:Show HN: Learn and practice modern JavaScript by __oh_es on Thursday January 03, 2019 @10:30AM (#18815784)
The reason I’ve used (and stuck to) single over double is it’s easier to embed JavaScript within html (html convention being double).

It can get cluttered when with nested quotes, but most linters will allow you to use the other type to encapsulate.

IMO it doesn’t matter, as long as the team agrees and remains consistent throughout the codebase.

Re:Show HN: Learn and practice modern JavaScript by arethuza on Thursday January 03, 2019 @10:37AM (#18815852)
Thanks - that's a decent explanation for favouring one over the other.

I rather dislike "best practices" that don't provide any explanation of what they are recommending.

Re:Show HN: Learn and practice modern JavaScript by jrockway on Thursday January 03, 2019 @10:44AM (#18815908)
I mean, it's because it doesn't matter. Picking one is valuable for consistency (no odd diffs when refactoring). Which one you pick doesn't matter.
Re:Show HN: Learn and practice modern JavaScript by ficklepickle on Thursday January 03, 2019 @08:54PM (#18821340)
The behavior is slightly different. "\n" is a newline, '\n' is a backslash and an n.
Re:Show HN: Learn and practice modern JavaScript by lauritzsh on Friday January 04, 2019 @05:36AM (#18823057)
Not in JavaScript. \n is treated as a newline in both cases. Try it out in the console.
Re:Show HN: Learn and practice modern JavaScript by chrisweekly on Thursday January 03, 2019 @02:00PM (#18818034)
... Unless you would prefer to skip the extra keypress required for ".
Re:Show HN: Learn and practice modern JavaScript by ergothus on Thursday January 03, 2019 @11:23AM (#18816256)
The sane argument is about avoiding escaping of nested quote characters.

It's easier to use double quotes if your text is english (or other language that uses apostrophes).

It's easier to use single quotes if your text is HTML (for double quotes)

I've seen an argument to use template literals (backticks) almost everywhere. I don't know if that involves any real performance hit, but I've also not adopted this convention, at least not yet.

Re:Show HN: Learn and practice modern JavaScript by metamet on Thursday January 03, 2019 @11:47AM (#18816505)
Template literals also have the benefit of being able to embed variables without concating the string, ie: `My name is ${name}.`
Re:Show HN: Learn and practice modern JavaScript by z3t4 on Thursday January 03, 2019 @10:49AM (#18815949)
An argument for " is you don't have to escape ' inside string's. In an argument about single vs double quote you can bring up `Template strings` and everyone will hate you ;)
Re:Show HN: Learn and practice modern JavaScript by LocalPCGuy on Thursday January 03, 2019 @04:40PM (#18819561)
For a slightly different reason than many I've seen here - double-quotes require the shift key to be depressed on standard keyboards. So programming with double-quotes doubles the number of keystrokes you need to type any time you quote something. Pretty sure the person that told me that was a Vim fanatic as well.

Not saying that it's a strong reason to use one over the other, but it is an objective reason that doesn't rely on style or preference.

Re:Show HN: Learn and practice modern JavaScript by chrisweekly on Thursday January 03, 2019 @01:57PM (#18818004)
I agree w/ other commenters that using a formatter like Prettier is a good idea. But to answer your question, single quotes don't require use of the Shift key so are a better choice for DX / ergonomics. RSIs are no joke.
Re:Show HN: Learn and practice modern JavaScript by mr_toad on Thursday January 03, 2019 @09:37PM (#18821525)
I don’t ever want to work with someone who insists on one style of quote marks in a language where it doesn’t make any difference.

There’s a difference between promoting readable code and just being retentive.

Re:Show HN: Learn and practice modern JavaScript by Klathmon on Thursday January 03, 2019 @10:28AM (#18815757)
Like most lint rules, it's mostly preference. The important part is being consistent within your team/project like you said.