-4

Accidentally stumbled on the comma separator while learning to properly use ternary operator (not used here).

Is there any sort of inherent issue I am missing when using the comma to separate statements or essentially put them on one line?

if (clicked.classList.contains("open")) {
  clicked.classList.remove("open"), clicked.classList.add("closed");
}

if (clicked.classList.contains("open")) {
  clicked.classList.remove("open");
  clicked.classList.add("closed");
}
Kevin Marmet
  • 172
  • 2
  • 9
  • 1
    Also, you should be coding for readability, so the second option would be your best bet. You get no gain from doing it the first way. – Derek Pollard Apr 20 '18 at 15:52
  • 2
    Method #3 - Same as #2 but with semicolons to explicitly terminate the statements. – Scott Marcus Apr 20 '18 at 15:52
  • @ScottMarcus ...its just an example not production code. Semicolons aren't relevant to my question. – Kevin Marmet Apr 20 '18 at 15:54
  • @Derek You are right, in this example. However, if I have say 10 lines (naive example) of adding and removing classes, this is much simpler to me, in my personal opinion, which is why I am asking. – Kevin Marmet Apr 20 '18 at 15:55
  • @KevinMarmet but they (semi-colons) are very relevant. Using them is best practice and not using them begins a bad habit – Derek Pollard Apr 20 '18 at 15:55
  • @Derek No, not relevant to my question, at all. – Kevin Marmet Apr 20 '18 at 15:57
  • 1
    @KevinMarmet it is not much simpler because in 3 years when you come back to refactor the code, you're going to have a hell of a time trying to conceptualize everything going on in just a single line – Derek Pollard Apr 20 '18 at 15:57
  • 1
    @kevinB i will vote to `reopen` for every bad dupe you mark on here. Please put at least some effort into it. – Jonas Wilms Apr 20 '18 at 15:57
  • 1
    @KevinMarmet They really are relevant and an important part of this discussion because `,` is a way of continuing a statement and `;` is a way of terminating it. Because of JavaScript's automatic semicolon insertion, your second method could become problematic if you don't use them. – Scott Marcus Apr 20 '18 at 15:57
  • 2
    Possible duplicate of [JavaScript variable definition: Commas vs. Semicolons](https://stackoverflow.com/questions/3781406/javascript-variable-definition-commas-vs-semicolons) – rlemon Apr 20 '18 at 15:58
  • L...O....L. This site.. – Kevin Marmet Apr 20 '18 at 16:00
  • 3
    @KevinMarmet This site? You're the one who came to us with an easily google-able softball, don't get mad at us for picking it apart. – Derek Pollard Apr 20 '18 at 16:01
  • 2
    @rlemon where do you see a variable definition here?! – Jonas Wilms Apr 20 '18 at 16:01
  • 1
    I'm not mad at anything. It is just this site. I asked a basic question and it is getting shredded for completely irrelevant reasons. I get what you are saying, and I have semi-colons in my actual code, just not in this specific example because it was an example. I mean if we are going to get picky, I should have shown you ALL of my code right? Because the code by itself is worthless. So maybe I should show you all of my production code right? People on this site are just seriously hell bent on picking people and their questions apart for little to no reason. Answer the question or don't. – Kevin Marmet Apr 20 '18 at 16:04
  • 1
    I just think you should understand that many of the people responding to your question know that, in JavaScript, omission of `;` is an anti-pattern and we are not in the habit of leaving it out in any code. We're not trying to be difficult, we just know what the ramifications are and have been "programmed" [ ;) sorry! ] to always include them. – Scott Marcus Apr 20 '18 at 16:10
  • @ScottMarcus I can appreciate that. But when I write code in my editor, I always use semi-colons. I just didn't assume it was a nuclear level issue for omitting them for a simple code example. – Kevin Marmet Apr 20 '18 at 16:12
  • 1
    It's not nuclear - I wasn't trying to irritate you. You have to understand that while you understood what you were writing, we don't always know what's on the mind of the poster. Also, please understand (and this is very important) that Stack Overflow is a knowledge base. **What we write here isn't just for you to get your answer. It's for others who will come after you and find this thread - - they might not be aware of the semicolon issue and so we always point these kinds of things out.** – Scott Marcus Apr 20 '18 at 16:15
  • Consider, you could just as easily replaced the semi colons with an addition operator and your code would have worked too. `clicked.classList.remove("open") + clicked.classList.add("closed")` Doing so does slightly alter the way the code would have ran in some cases (as does using a comma), but in your case it's identical. The reason semi-colons keep getting brought up is you're replacing their usage by using an operator... When declaring variables you can save a little bit of file size by doing that, but with code minification now days that's irrelevant too. – Kevin B Apr 20 '18 at 16:16
  • updated the code -- added semi-colons, IMO that illustrates the point that for EXAMPLE code, the semi-colons were not necessary. – Kevin Marmet Apr 20 '18 at 16:16
  • 1
    tldr instead of using end of line or semicolons to separate your expressions, you used a comma. it's nothing more than stylistic in your case, but can have negative side effects in other cases. – Kevin B Apr 20 '18 at 16:18
  • 5
    @ScottMarcus I want to apologize. After reading your initial comment a few more times, you are right. Which you already knew of course. I have had a crap day, but that is no excuse. I am use to asking simple questions and getting shredded on this site, so I overreacted. That is on me. But instead of letting it sink in I read Dereks first post which wasn't about my question and was about "best practices" (shut up) and that sent me through the roof. So my bad Scott! Appreciate the help. – Kevin Marmet Apr 20 '18 at 16:26
  • 1
    @KevinMarmet No prob. Good luck! – Scott Marcus Apr 20 '18 at 16:29

1 Answers1

3

The main issue IMHO is that the meaning of comma is dependent on context.

If you're in a function call, comma is used to separate arguments:

f(arg1, arg2);

If you want to use the comma operator to separate expressions in a single argument, you need an extra set of parentheses:

f((arg1, arg2));

And in variable declarations, it separates variables being declared:

let v1 = foo, v2;

is a declaration of two variables, it's not the comma operator; again, you'd need parentheses:

let v1 = (foo, v2);

More generally, comma is not idiomatically used to separate statements. It's useful when you need to sequence multiple operations in a place where only one statement is allowed, e.g. the header of for() or while(). But top-level statements should be separated with semicolon, not comma. Other programmers will find it easier to read your code if you follow common conventions like this.

I wouldn't be surprised if the comma operator is used more often in programming puzzles and quizzes than in actual production code.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • "More generally, comma is not idiomatically used to separate statements" - Thank you. that is literally all I was looking for..... – Kevin Marmet Apr 20 '18 at 16:06
  • 1
    @KevinMarmet it was said within the first comment of your original post – Derek Pollard Apr 20 '18 at 16:07
  • 2
    ...and then you repeatedly kept saying how it's not relevant to your question. – JJJ Apr 20 '18 at 16:09
  • @JJJ ??? No. I said that the usage of semi-colons are not relevant. If you are going to insult someone, at least get the facts..... – Kevin Marmet Apr 20 '18 at 16:10
  • 3
    I mean, they are relevant, considering you're replacing them with commas in this case. – Kevin B Apr 20 '18 at 16:10
  • 1
    @KevinMarmet Nobody is insulting anyone, they are simply trying to educate you on why your logic was flawed in this situation. It's a learning experience, not a fight. – Derek Pollard Apr 20 '18 at 16:11
  • @Derek Logic is not flawed. Simply wanted to know whether using a comma to separate statement was good or bad practice. That is all. My logic is not flawed because I didn't add semi-colon to EXAMPLE code. We can keep going.... – Kevin Marmet Apr 20 '18 at 16:13
  • 1
    I believe what they're saying is not relevant is the issue of using semicolons explicitly versus depending on automatic semicolon insertion. – Barmar Apr 20 '18 at 16:13
  • 1
    @KevinMarmet On that note, I think ASI is evil. There have been quite a few SO questions where the problem was that ASI did something surprising to the programmer. – Barmar Apr 20 '18 at 16:14
  • @Barmar exactly. Especially when replacing the semicolon with a comma. in some situations, doing that could result in a not-so-obvious error – Derek Pollard Apr 20 '18 at 16:14