Slide 1

Slide 1 text

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.

Slide 2

Slide 2 text

ACT I Forms & Voids Programming languages are amazing and powerful. By typing on our keyboards we can express all kinds of logic and ideas.

Slide 3

Slide 3 text

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:

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

; 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.

Slide 7

Slide 7 text

{ 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:

Slide 8

Slide 8 text

{ tunez: "chiptune" , mood: "excited" } Or this.

Slide 9

Slide 9 text

{ 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.

Slide 10

Slide 10 text

[ 'A', 'B', 'C', ].length // 4 It also can lead to sometimes confusing behavior in older versions of JS like this.

Slide 11

Slide 11 text

function rousingApplause() { return "YAAA, RAAAA, " } rousingApplause() // undefined Or this. Crap.

Slide 12

Slide 12 text

function rousingApplause() { return "YAAA, RAAAA, " } rousingApplause() // "YAAA, RAAAA, " There we go.

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

(1 2 3 pi 4.5) > (1 2 3 3.1415 4.5) LISP is one example. Here's an array of numbers.

Slide 17

Slide 17 text

(+ 1 2 3 pi 4.5) > 13.6415 And here's a function call.

Slide 18

Slide 18 text

(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.

Slide 19

Slide 19 text

{ me { name age location } } GraphQL also works this way. In GraphQL there's also no ambiguity, so commas and semicolons are unnecessary!

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

(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.

Slide 23

Slide 23 text

(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.

Slide 24

Slide 24 text

{ 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.

Slide 25

Slide 25 text

{ tunez: "chiptune", mood: "excited", } As this.

Slide 26

Slide 26 text

{ 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.

Slide 27

Slide 27 text

,, ,, ,, ,, ,,,,,, ,,,,,, ,,,,,, ,,,, ,,,,, ,,,, ,,,, ,,,,,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, , ,, ,,,,,, ,, ,, ,,,, ,,,, ,,,,, ,,,,,, ,, ,,,, ,,,,,,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,, ,,,,,, ,, ,,,,,, ,,,, ,, ,, ,, ,,,, ,,,,,, 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.

Slide 28

Slide 28 text

Lee Byron @leeb