Semicolons cannot save you! - FunFunFunction #9

Fun Fun Function · Intermediate ·🏗️ Systems Design & Architecture ·10y ago

Key Takeaways

Discusses the role of semicolons in JavaScript, highlighting their limitations and the importance of understanding automatic semicolon insertion

Full Transcript

this is fun fun function a Monday Morning Show where we try to become more excited and confident about programming by exploring old wisdom wild ideas and having fun some viewers a few have commented on the fact that I like to Omit uh semicolons from my JavaScript instead of writing the same comment over and over again I'd like to talk a little bit about that today let's get one thing straight here before we continue you I do not recommend that you omit semicolons nor do I recommend that you should use them I will love you just as much if you omit them like GitHub does or if you use them as terminating statements on every line like we do at Spotify if somebody tells you that it is a general best practice to use semicolons in JavaScript that person is uh lying there is no Central Authority setting best practices for Javas script best practice that's what you and your team decides is best practice so what do you base that decision on then well a lot of teams prefer to use semicolons just well because it's what they ended up doing the team needed a consistent rule for using semicolons or not and they just opted for using them I think that is a perfectly valid reason consistency is very nice and important other teams might use semicolons because uh they might be switching a lot between a c codebase and JavaScript codebase and uh having semicolons makes the switch less jarring and that's an okay reason too in my book contact switching is expensive and if we can make that easier then that's a good thing by now you should have sense that there is a big hairy butt coming up here but in my opinion there is one invalid reason for using semicolons and it is unfortunately also a very common one and that is because there are a lot of people well meaning people going around spreading misinformation and fear saying that using semicolons protects you from errors related to automatic semicolon insertion we will call those people wrong so in case you're new to JavaScript the language has this feature called automatic semicolon insertion often referred to as ASI automatic semicolon insertion does what it sounds like it does if you don't add a uh semicolon at the end of a line The Interpreter will simply add one for you while parsing your code mostly this works but sometimes it results in some very nasty errors uh like this one uh this is usually one of the first ones that you run into when you start coding JavaScript uh the first function here will return four for while the second one will return undefined and that is because The Interpreter will interpret the code uh by adding semicolons like this do you see how Sinister this is almost everyone agrees that automatic semicolon insertion is a stupid feature it should not be in JavaScript it it is a super dumb feature related to this there is a very common advice given and that advice is that you should not rely on automatic semicolon insertion and to me that that advice it I I don't think it makes sense it would be perfectly good advice if the automatic semicolon insertion could be turned off but it can't be turned off the ASI is there it's it's there to stay it's in The Interpreter it's in the standard and it's going to try to help you and do its stupid things all the time giving this advice kind of implies that you can get rid of it somehow and Dodge but you can't let's take a look at the last function the one with the as ER but uh here we have added a semicolon at the end you know like correctly and this will still return undefined because The Interpreter will just do this this the semicolon will not help you adding semicolons everywhere will not absolve you from thinking about the ASI not at all this is Javascript it's a great little language but the ASI that's one of its words and you're going to have to deal with it you cannot semicolon it away as a JavaScript programmer you cannot be absorbed of your responsibility to constantly think about the ASI just by adding a semicolon rule to your witer the only situation where having uh semicolons everywhere will save you from an ASI error is when you begin a line with a parenthesis or a square bracket by the way uh this example was sent to me by uh thodoris uh he is he's This brilliant Greek man working at Spotify and he disagrees with me vehemently on this issue uh you should really follow him on Twitter at uh 7 two lions he's awesome as you see here uh the result is just unimaginable garble uh I'm not even sure exactly how this is messed up but adding a semicolon at the end of the first line like this will actually change the execution of the code completely and that is square brackets uh but it's the same thing when you start a line with a parenthesis like this and that will be evaluated as this beginning a line with a square bracket or a parenthesis are the only cases that I know of where adding semicolons uh will save you from ASI errors all the other possible ASI errors will still hit you even if you add semicolons everywhere you're basically adding a character to every single line in your code base in order to protect you from this case and in my personal EXP experience this is just not one of the common aiders and when it arises it's often quite obvious what has happened again I have nothing against people using semicolons I just have a problem with people saying that it's a tool to protect against ASI errors because it is a terribly bad tool for protecting against ASI errors only learning how ASI Works will protect you from asir by all means use semicolons for consistency or just because your team likes it that way because it's more seal likee or just because you like how it looks or find it more readable with semicolons but don't buy into the religion that it absolves you from thinking about automatic semicolon insertion that is just a false sense of security the ASI is always with you and you as a JavaScript programmer will always need to be vigilant no matter if you add semicolons or not I have one more thing to say but I um I need to show you this isn't it awesome I don't know if you can see it but this this is um this is an old cross stitch and this is lechu from Monkey Island made with fusible plastic beeps this is so awesome this is made by Papa pal uh and I'm a huge fan of him this video is running a bit longer than I would like it to but I I just want to finish up with one one more thing because I feel it's important this semicolon thing is an incredibly common comment on my videos even on videos where there is a obvious bug in my code there are more people commenting about the lack of semicolons than the bug I I think it's kind of important in um in the workplace and on the internet too to before before raising an issue to ask yourself will raising this issue actually add some value or am I raising this issue just because it's really easy to have an opinion about it and I know that it is very ironic to say this considering that I made an entire video about this issue but this issue semicolons or no semicolons is not important mandatory code review that is something that is important error logging is important continuous in integration that is important semi Collins H not so much in the grand scheme of things that's all I had to say as usual the next episode will be out next Monday morning GMT time don't miss out subscribe and until next Monday stay curious

Original Description

💖 Support the show by becoming a Patreon https://www.patreon.com/funfunfunction By all means, use semicolons for consistency, or just because your team likes it that way because it’s more C-like, or frankly just because you like how it looks. But don’t buy into the religion that it absolves you from thinking about automatic semicolon insertion. The ASI is always there, and and you need to be always vigilant about it, no matter if you add semicolons or not. Full script here: https://medium.com/@mpjme/bf991756174e More reading: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fun Fun Function · Fun Fun Function · 20 of 60

1 Higher-order functions - Part 1 of Functional Programming in JavaScript
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
2 Map - Part 2 of Functional Programming in JavaScript
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
3 Reduce basics - Part 3 of Functional Programming in JavaScript
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
4 Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
5 Reduce Advanced - Part 4 of Functional Programming in JavaScript
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
6 Closures - Part 5 of Functional Programming in JavaScript
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
7 Too many tools and frameworks!
Too many tools and frameworks!
Fun Fun Function
8 Currying - Part 6 of Functional Programming in JavaScript
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
9 Recursion - Part 7 of Functional Programming in JavaScript
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
10 Promises - Part 8 of Functional Programming in JavaScript
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
11 Staying relevant as a programmer
Staying relevant as a programmer
Fun Fun Function
12 Factory Functions in JavaScript
Factory Functions in JavaScript
Fun Fun Function
13 Composition over Inheritance
Composition over Inheritance
Fun Fun Function
14 Software needs to be better - FunFunFunction #1
Software needs to be better - FunFunFunction #1
Fun Fun Function
15 Unit testing: How to get your team started - FunFunFunction #2
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
16 Straight-line code over functions - FunFunFunction #3
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
17 Clojure - FunFunFunction #5
Clojure - FunFunFunction #5
Fun Fun Function
18 The growth stages of a programmer - FunFunFunction #6
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
19 5 tips to quickly understand a new code base - FunFunFunction #7
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
Semicolons cannot save you! - FunFunFunction #9
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
21 Functors - FunFunFunction #10
Functors - FunFunFunction #10
Fun Fun Function
22 Functors: I was WRONG! - FunFunFunction #11
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
23 Questions and Answers - FunFunFunction #12
Questions and Answers - FunFunFunction #12
Fun Fun Function
24 Streams - FunFunFunction #13
Streams - FunFunFunction #13
Fun Fun Function
25 Prototypes in JavaScript - FunFunFunction #16
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
26 Fast or Flexible? - FunFunFunction #17
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
27 Coders are herd animals - FunFunFunction #18
Coders are herd animals - FunFunFunction #18
Fun Fun Function
28 Weekend Kubernetes Shenanigans - FunFunFunction #19
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
29 Monad - FunFunFunction #21
Monad - FunFunFunction #21
Fun Fun Function
30 Moar Weekend Shenanigans - FunFunFunction #23
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
31 Questions and Answers - FunFunFunction #24
Questions and Answers - FunFunFunction #24
Fun Fun Function
32 Losing motivation - FunFunFunction #25
Losing motivation - FunFunFunction #25
Fun Fun Function
33 LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
34 Fast code is NOT important - FunFunFunction #27
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
35 Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
36 Writing unit tests for personal projects? - FunFunFunction #29
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
37 Let's Code a Pomodoro Button - FunFunFunction #30
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
38 What editor do you use? - FunFunFunction #31
What editor do you use? - FunFunFunction #31
Fun Fun Function
39 Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
40 Is Programming Art? - MPJ's Musings - FunFunFunction #33
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
41 Generators in JavaScript - What, Why and How - FunFunFunction #34
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
42 Haskell Basics - FunFunFunction #35
Haskell Basics - FunFunFunction #35
Fun Fun Function
43 Haskell - Baby's first functions - FunFunFunction #36
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
44 Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
45 How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
46 Haskell lists - FunFunFunction #39
Haskell lists - FunFunFunction #39
Fun Fun Function
47 var, let and const - What, why and how - ES6 JavaScript Features
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
48 Why are some programming languages popular? - MPJ's Musings  - FunFunFunction #41
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
49 Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
50 bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
51 Examples of this and bind - Object Creation in JavaScript P2 -  FunFunFunction #44
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
52 Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
53 Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
54 Cellular Automata - Pair Programming - FunFunFunction #49
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
55 The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
56 __proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
57 Unity game pair programming - Let's code - FunFunFunction #53
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
58 Throw out your tools - MPJ's Musings - FunFunFunction #54
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
59 Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
60 Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function

Related Reads

📰
Architectural Breakdown: A Storm Didn't Decide This Battle — A Sergeant Did
Learn how a sergeant's intervention fixed a broken tactics engine, applying problem-solving skills to software development
Dev.to · Muhammad Hammad
📰
Coding Is Getting Easier. But System Design Still Matters.
Learn why system design is crucial in software development, even with AI-powered coding tools
Dev.to AI
📰
Domain modeling: nine businesses hiding in one restaurant app
Learn to identify and model multiple domains within a single business, like a restaurant app, to improve software development and maintenance
Dev.to · Divyakush Punjabi
📰
OOP in C#: How Encapsulation, Inheritance, Polymorphism & Abstraction Actually Show Up in ASP.NET
Learn how OOP principles like encapsulation, inheritance, polymorphism, and abstraction are applied in ASP.NET
Medium · Programming
Up next
What Is Edge Computing? Explained Simply
Coursera
Watch →