Semicolons cannot save you! - FunFunFunction #9
Skills:
Systems Design Basics80%
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
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 20 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
▶
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
Too many tools and frameworks!
Fun Fun Function
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
Staying relevant as a programmer
Fun Fun Function
Factory Functions in JavaScript
Fun Fun Function
Composition over Inheritance
Fun Fun Function
Software needs to be better - FunFunFunction #1
Fun Fun Function
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
Clojure - FunFunFunction #5
Fun Fun Function
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
Functors - FunFunFunction #10
Fun Fun Function
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
Questions and Answers - FunFunFunction #12
Fun Fun Function
Streams - FunFunFunction #13
Fun Fun Function
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
Coders are herd animals - FunFunFunction #18
Fun Fun Function
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
Monad - FunFunFunction #21
Fun Fun Function
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
Questions and Answers - FunFunFunction #24
Fun Fun Function
Losing motivation - FunFunFunction #25
Fun Fun Function
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
What editor do you use? - FunFunFunction #31
Fun Fun Function
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
Haskell Basics - FunFunFunction #35
Fun Fun Function
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
Haskell lists - FunFunFunction #39
Fun Fun Function
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function
More on: Systems Design Basics
View skill →Related Reads
📰
📰
📰
📰
Architectural Breakdown: A Storm Didn't Decide This Battle — A Sergeant Did
Dev.to · Muhammad Hammad
Coding Is Getting Easier. But System Design Still Matters.
Dev.to AI
Domain modeling: nine businesses hiding in one restaurant app
Dev.to · Divyakush Punjabi
OOP in C#: How Encapsulation, Inheritance, Polymorphism & Abstraction Actually Show Up in ASP.NET
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI