Press "Enter" to skip to content

Semicolons cannot save you! – FunFunFunction #9


this is fun fun function a Monday

morning show where we try to become more

excited and confident about programming

by exploring all wisdom wild ideas and

having fun some viewers a few have

commented on the fact that I like to

omits 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 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 of never

lying like we do at Spotify if somebody

tells you that it is a general best

practice to use semicolons in JavaScript

that person is lying there is no central

authority setting best practices for

JavaScript as practice that’s what you

and your team decides is best practice

so what do you base that decision on

them 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

they might be switching a lot between AC

code base and JavaScript code base and

having semicolons makes the switch less

jarring and that’s an okay reason to in

my book context switching is expensive

and if we can make that easier and

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 while 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

a si automatic semicolon insertion does

what it sounds like it does if you don’t

add a 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 like this one yeah

this is usually one of the first ones

that you run into when you start coding

javascript the first function here will

return for while the second one will

return undefined and that is because the

interpreter will interpret the code R 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 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

advised it 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 a si 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 it’s stupid things all the

time giving this advice kind of implies

that you can get rid of it somehow and

but you can’t let’s take a look

at the last function the one with the a

si R but ah here we have added a

semicolon at the end you know but

correctly

and this will still return undefined

because the interpreter will just do

this the semicolon will not help you

adding semicolons everywhere will not

absorb 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

warts 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 lender

the only situation where having

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 this example was sent to me

by

Fedora’s he is this brilliant Greek man

working at Spotify and he disagrees with

me vehemently on this issue

you should really follow him on Twitter

at 72 lines he’s awesome as you see here

the result is just unimaginable garble

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 but it’s the same thing when

you start aligned 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 we’re adding

semicolons will save you from asi errors

all the other possible asi RS will still

hit you even if you add semi-colons

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 experience

this is just not one of the common

insiders and when it arises it’s of

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 ASIS only learning how ASI works

will protect you from a hires by all

means use semicolons for consistency or

just because your team likes it that way

because it’s more C like 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 I need to show

you this isn’t it awesome I don’t know

if you can see it but this this is this

is an old cross stitch and this is

lechuck from Monkey Island made with the

fusible plastic beeps this is so awesome

this is made by Papa’s palü 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 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 I

think it’s kind of important in in the

workplace and and only internet 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

it’s not important mandatory code review

that is something that is important

error logging is important continuous

integration that is important semicolons

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

Please follow and like us: