Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Commas as Whitespace

Lee Byron
September 08, 2016

Commas as Whitespace

I think commas and semicolons should be considered whitespace, and I'll explain what I mean in four acts.

Lee Byron

September 08, 2016
Tweet

Other Decks in Programming

Transcript

  1. Commas as Whitespace Lee Byron @leeb After my talk earlier

    today someone asked me the question: "Y U no use semicolon in ur Java Scrip." A fantastic question that I thought required a more thoughtful response, and in fact this is actually something I've thought a lot about lately as I was designing the GraphQL language. I think commas and semicolons should be considered whitespace, and I'll explain what I mean in four acts.
  2. ACT I Forms & Voids Programming languages are amazing and

    powerful. By typing on our keyboards we can express all kinds of logic and ideas.
  3. function fibonacci(n){if(n<=2){return 1;}else {return fibonacci(n-1)+fibonacci(n-2);}} That's because programming languages use

    punctuation to provide structure to all the other ABCs and 123s. But there's another super important part of programming languages that perhaps we don't think about as much. It's the negative space, the voids between the forms:
  4. function fibonacci(n) { if (n < 2) { return n;

    } else { return fibonacci(n - 1) + fibonacci(n - 2); } } Code is written to be read, not just by your computer but far more importantly by you and me. Whitespace helps you improve legibility to see the logic in your code, and it's often totally up to the programmer as to how to use it.
  5. ACT II Expressions, Expressions; Expressions When we write programs, we're

    almost always writing lists. Lists of values in an array, lists of arguments to functions, and lists of statements for the computer to execute.
  6. ; LIST ; PROCESSOR (defun fib (n) (if (< n

    2) n (+ (fib (- n 1)) (fib (- n 2))))) One of my favorite families of programming languages and in fact one of the oldest is LISP, which means List Processor. In fact all you can do in LISP is write lists.
  7. { tunez: "chiptune", mood: "excited" } But most of the

    languages we use derive from C and use commas and semicolons for lists of things. That leads to us fighting over whether we should do this:
  8. { tunez: "chiptune", mood: "excited", } Or ultimately fighting to

    change the language to make it possible to do this. And all this just to balance between legibility and maintaining blame lines in version control.
  9. [ 'A', 'B', 'C', ].length // 4 It also can

    lead to sometimes confusing behavior in older versions of JS like this.
  10. var inBag = purchaseBowtie() (temp = onNeck, onNeck = inBag,

    inBag = temp) console.log(onNeck) // TypeError: purchaseBowtie(...) is not a function Or this, what is even happening here.
  11. var inBag = purchaseBowtie(); (temp = onNeck, onNeck = inBag,

    inBag = temp) console.log(onNeck) // "Polka Dot Diamond-point" Oh whoops, here we can fix it. Commas and Semicolons are significant and whether you include them or not can change the meaning and behavior of your code or lead to syntax errors. This leads to developer mistakes, bugs, and less legible code.
  12. ACT III Let's eat my friends There are a few

    languages out there which have fully side-stepped the problems commas and semicolons present by omitting them completely.
  13. (1 2 3 pi 4.5) > (1 2 3 3.1415

    4.5) LISP is one example. Here's an array of numbers.
  14. (do (wakeUp "9:00") (eatBreakfast "coffee" "9:30") (getBackInBed "11:00")) And here's

    a list of expressions to run. There's no ambiguity about where one item in the list ends and the next begins, so there's no reason for commas or semicolons.
  15. { me { name age location } } GraphQL also

    works this way. In GraphQL there's also no ambiguity, so commas and semicolons are unnecessary!
  16. C & JS need commas for disambiguation. Avoiding ambiguity can

    be a challenge. As a language designer, this can actually be a pretty interesting challenge to make work! You have to be careful to avoid the kinds of ambiguity problems that languages like C and JavaScript suffer from. If done right, that means less things can be accidentally misinterpreted, which means fewer bugs and less confusion.
  17. ACT IV Forms, Voids & Lines Back to whitespace. Even

    though most languages ignore whitespace when running your code, we understand it still serves a very important purpose of helping us to read and understand that code. But whitespace isn't the only thing that can make code legible. Sometimes commas and semicolons can actually make it easier for us to understand the purpose of code. So we don't actually want to get rid of them, we just want to treat them like whitespace: not significant for running a program, but still available for you to use to improve legibility when relevant or to omit if they're simply noise.
  18. (1 2 3 pi 4.5) > (1 2 3 3.1415

    4.5) In fact both GraphQL and most versions of LISP allow you to do just this: Let's take another look at that array of numbers in LISP.
  19. (1, 2, 3, pi, 4.5) > (1 2 3 3.1415

    4.5) Maybe it would be easier to read if we just used commas. Most LISP languages let you write this, even though it means exactly the same thing.
  20. { tunez: "chiptune" , mood: "excited" } And it could

    help us just leave the trailing comma problem up to your particular style which could make it equally valid to write this.
  21. { tunez: "chiptune" mood: "excited" } Or maybe even just

    this. Commas could be there purely as a legibility aid so you could decide which is most legible without fear of changed behavior.
  22. ,, ,, ,, ,, ,,,,,, ,,,,,, ,,,,,, ,,,, ,,,,, ,,,,

    ,,,, ,,,,,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, , ,, ,,,,,, ,, ,, ,,,, ,,,, ,,,,, ,,,,,, ,, ,,,, ,,,,,,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,,,,,, ,, ,,,,,, ,,,, ,, ,, ,, ,,,, ,,,,,, So if you work on a programming language, are thinking of starting one, or are just a language hobbyist - consider this idea that commas and semicolons should perhaps just be whitespace.