In this talk I will try to show you what the language has been trying to tell us for all these years, where the real path to enlightenment is, or at least we can have fun and look at things we know (or pretend to 😉 from an unorthodox point of view. 😉
Something we’ve always enjoyed in the aftermath of Web Directions conferences is reading the blog posts and articles by attendees that follow. There’s always half a dozen or more, and they act not only as record of what took place that’s useful to those who didn’t attend, but also as a kind of a filter that lets us see our events through others’ eyes.
Most people outline broad conference highlights or focus on a handful of the presentations and the insights gleaned from them, but for many years now there has been this one post-conference blog post we’ve all come to look forward to: Ben Buchanan’s Big Stonking Post™.
Partly in acknowledgement of all those people who write about their experiences at our events, and partly in admission that our usual Wrap format just wouldn’t capture Dmitry Baranovskiy’s talk, we’re turning this one over to Ben, re-published with permission from the 200ok weblog.
Those who are unaware they are walking in darkness will never seek the light. – Bruce Lee
Zen has the well-known conundrum of ‘what is the sound of one hand clapping’… in JS the question is probably ‘what number is not a number’. We are afraid of NaN!
What if NaN is there for a reason? What if we used it? If we have a function that returns a number but returns null if there’s an error… why not return NaN instead of null?
People are afraid of NaN because the first time you get NaN it’s because you fucked up. So from then on you think if you see NaN you did something wrong.
Accept NaN. It’s there. It exists. You can use it.
Dmitry used NaN in real code twice last week… he’s not saying his colleagues appreciated it.
== is hated… why so much hate? Dmitry’s current team has a rule you cannot use ==. There are reasons for it. There are unexpected and confusing results when you use ==. So we use ===.
=== is a true friend, always telling you what you want to hear.
But === is not fixing the problem, it’s covering the problem.
What about <= >= < > …? Not using these is like hiding by covering your face. If you don’t understand what you’re doing, you are in trouble.
.charAt() doesn’t care what you pass in to .charAt(). Be like .charAt(). It always returns something. Exceptions? Why throw exceptions? Why be so rude?
It doesn’t care what it’s running on, either. Pass it a number, it returns a number. Pass it NaN, it returns a character from NaN.
NaN.charAt() → “N”
Undefined isn’t even null. It’s more empty than null.
There is nothing new.
RegExp() and new RegExp() → the result is the same. Function() and new Function→ the result is the same
But then you have things like:
Date() is not equal to new Date(). Same for Number(), Set(), Symbol().
The five faces of function.
You can use Function five ways. You can call a function, you can return a function, you create contexts and closures with functions, you can use function as a constructor, you can call it with strings or numbers.
Is it a good thing, or a bad thing? Dmitry doesn’t know. But it’s how the language is written.
And those who were seen dancing were thought to be insane by those who could not hear the music. – Nietzsche
Be the ones who hear the music.
Opening title Next up we have someone who’s been very dear to the Web Directions world and family for many, many years. So he first came to Web Directions I guess probably around 2006 or seven, certainly very early in the days of the conferences, and I’m gonna let you in on a little secret. At one point, Dmitry said, “I have this ambition. “I want to speak on a Web Directions stage.” Right? So one of the things we used to do back in the day was to have different people emcee different sessions as an opportunity to get on stage.
We do lots of other things now.
We do, What Do You Know.
We run speaker development programs and other things to help people get on stage. And of course there are many more opportunities now. Lots of different conferences and meet-ups and all that sort of thing that really didn’t exist back then.
But I remember that quite clearly.
Not only did Dmitry end up speaking at Web Directions, but he spoke on a number of occasions, and he spoke in fact, all over the world, and now he is a senior software engineer, sorry, senior computer scientist at Adobe working on some very interesting things there. You may know him from such amazing libraries as Rafael and Snap SVG.
(audience applause) – Okay, so, yeah.
I’m probably, probably can win the contest for the most awkward photo for the conference.
I think John just picked up the worse photo of me on the internet for the conference.
I’m pretty sure on purpose.
(scattered laughter) Because if you just Google my name, there’s like plenty of photos of me, and this. (laughter) I did it on– I not always Google my name but when I do, (laughter) I search for the good photo of me.
(laughter) It’s not actually as hot as you could think by looking at my face.
(laughter) But John succeeded to find the worst.
If you been on the website and saw this.
That’s been said.
What I want to talk about.
I’m that old.
It was 1997, I think.
They going through like, “Yeah. Easy. I grab it. I get it.
Closures. Got it. Types. Got it. Prototypes. Got it.
See, see, see? – We’ve seen it before.
It’s easy to see.
The function names a bit long.
(laughter) It’s not my job.
It was done for you– it was built in such a way that you cannot get wrong in the way you’re writing.
You can still create shitty software, don’t worry. (laughter) But, there is no wrong way.
Any way is good.
I just want to give you little sparkle in your head maybe to bring you some ideas.
Those who are unaware they are working in darkness will never see the light.
Whose quote is that? I should say I have a prize, but I don’t.
(laughter) It’s Bruce Lee.
And then somewhere later in the code you have something like this.
If it’s in range, not equal to null, then do something, something, something.
Instead of including null into all this and then checking for the null or not null, or something like that, why not reach on NaN and put NaN into the game.
People afraid of NaN.
It’s like– I understand why.
I know why.
Because first time you met NaN, it was because you fucked up.
(laughter) Somewhere in your code, you messed up and you had strings to numbers and then it’s get NaN and you get NaN as a result and you’re like, “Oh!” And you’re avoiding it.
And every time you write in code, if you see NaN it means something wrong.
(laughter) It’s there.
It’s in there.
You like it, you don’t like it.
It’s still there.
Why not embrace it, instead? So if your function reach a NaN like this, you can pass it over and you don’t need to check for anything.
It’s a number.
You can still multiplicate by it.
You can still do some stuff.
At the end, you can check if it’s NaN and do something. I’m not saying this is the right way and you’re writing the wrong way.
Just think about it.
It’s there for a reason.
Why not to use what already exists in the language? Maybe this is the way of language.
It’s either good implement, very well implemented or it’s very bad implemented.
Depends how you look at it.
A most common and loved operator is double equal, right? Everybody has love hate relationship with double equals. (laughter) I don’t know.
I like this operator.
Everybody trying like, “Yeah, yeah, yeah.
“Don’t use it. Don’t use it because this.
“Object equals true? False.
“Object equals false? False” Doesn’t make sense.
But if you put wel-dee off in the object which returns zero, object equals false? True.
But array equals false. Always true.
(laughter) Here I’m pretending I don’t know why.
And of course, then equals null, false.
(laughter) And when people are saying, “Okay, this is rubbish, this insanity.
“Don’t use it.” Let’s use triple equals.
This is our true friend.
He always tells you what you want to hear.
(laughter) But using triple equals instead of double equals, it’s like playing hide and seek and just covering your eyes.
You’re like, “Oh you can’t see me, “therefore I can’t see you, so you can’t see me.” It’s very childish.
(laughter) Because what about these guys? Okay, you don’t use double equals anymore.
You ee-es-linked it all out from your code. You don’t use this as well, right? Because, you know what, they behave exactly the same way. So if you get rid of double equals, get rid of all his friends.
Let’s look, for example, at lesser or greater. Less or equal.
Whatever this called.
(laughter) I’ve seen it somewhere.
Object lesser equal true? False Object lesser equal false? False.
Oh my god, you do the same with the way you’re off you had the rate, you have the same.
I showed it to friend of mine and he said, “Yeah, I got it, I got it.” It can slow or equal, it converts everything to number. Look, look, look, look.
Object becomes NaN, two becomes one.
NaN always returning false.
In the second again, NaN zero false.
Then you get while the off it which on zero, zero and zero. True.
Array, array, array, Okay, array.
Array occurring the object to number, it converted to primitive, so it’s connected to string, empty string, it becomes zero.
And forces zero.
Ah-ha! It’s number.
It just works like with numbers.
No, not really.
(laughter) This is very common case.
People build theories.
Then they believe in their theories.
Then they code based on their theories.
And their theories are wrong.
Don’t build stupid theories.
(laughter) There are no secrets.
There is a specification.
It’s not very well written, but it’s not very terrible. You can read it.
It’s written in English.
You should be able to read it.
This is not treated like numbers.
Why this is like that? It’s your whole milk.
Acceptance. Very important.
It’s what language is built on.
It’s very good kind language.
It accepts anything.
All your hate, all your frustration, everything gets accepted.
It accepts it, too.
But if you want to try to write acceptable code, and when I say acceptable, I’m not talking about accessibility or stuff like that, be nice.
Look at charAt.
It exists in the core.
Let’s see how it behaves.
Well, charAt gives you a method of the string object and it gives you back the character in a particular place.
It doesn’t give a shit what you pass in.
You pass in string? It converts to number.
You pass float number.
It understands you mean integer part of it. You pass array, it converts to number.
You pass through, it converts it to number. You pass nothing, it assumes it’s zero.
You pass NaN, it assumes it’s zero.
Because NaN doesn’t make sense here.
You pass negative, it just returns your empty string. Exceptions? Why be so rude? Why exceptions? (laughter) Even more, you put this form string prototype on number prototype, it doesn’t care.
It doesn’t care what the context it’s running in. You put it on bullion.
Okay, bullion, whatever.
(laughter) charAt doesn’t care what you’re passing in. charAt doesn’t care what it’s running upon. charAt just rocks.
Be like charAt.
(laughter) It’s a life lesson here in a couple lines of code.
There is very common, again, concept of zen called emptiness.
We, as a Western culture, always trying to fill the emptiness and fill the silence.
That’s what we’re living for.
In Asian culture, they’re sort of embracing this emptiness.
Then come two minds.
Then what is it? Nothing.
Now, even more nothing.
(laughter) Undefined. Not even now.
(laughter) As empty as it gets.
And you think okay, this is the bottom.
In Sydney, I was stuck with people during this flight. Array of two, when you create array of two, is equivalent to creating array like this.
Basically if you write like this, it’s true. Makes sense, easy peasy.
So we create a, which is array of two.
B, which is another way of creating array of two. Two undefines.
A of zero, triple equals– I use triple equals for you guys (laughter) A of zero triple equals b of zero.
A of one triple equals b of one.
A length triple equals b of length.
They are the same, right? Of course not.
(laughter) b.map alert, you have two alerts.
a.map alerts, no alerts.
Home come? Check a in a, one in a, false.
One in b true.
What that means? Well that means that in array b, you have array inside this array.
There are two place holders and in that place holders two values undefined and undefined.
Array a has just two place holders.
There is nothing in there.
(laughter) Not even undefined.
Okay? Zero. None, not even zero.
Null, not even none.
Undefined, not even null and this is not even undefined. It doesn’t even have a name.
(laughter) The emptiness of emptiness.
(laughter) Think about it.
We like this crazy mixture of fashion and engineering, which looks like hipsters and stuff.
So we make things to look not necessarily good, but fashionable.
And originally, there were times when people were writing and initiating objects in the race like that.
If you’re old enough, you remember.
Old coders remember.
(laughter) I wasn’t preparing this one.
This is true for object.
This is true for array.
This is true for RegExp.
This is true for function.
There are exceptions, like date.
It was copied from Java.
That’s why when you’re working as date, it always looks like it has two left legs.
(laughter) It’s very awkward.
Number and other primitive ropers they also look different when you code then as out new. Basically they work like in Java to convert one type into another.
Set and new set.
Not only set is not equal to new set.
When you call set without new, it’s throwing an exception.
With symbol, it’s opposite.
They’re not coming together.
These are two opposite things from two different universes.
No division by zero.
No, “Oh my god, you divided by zero.” No, everything’s fine.
(laughter) It’s infinity.
It’s all about acceptance.
It’s all about forgiveness.
There are also the concept of using the same thing for many different ways.
Like the function.
So it uses just a piece of code you can run these arguments and get some results.
You could also reach on or pass functions around because it’s also an object.
Every time you create a function, you create different context.
Every time you create a function, you create the closure and I don’t want to go into what this is all about. And obviously you can use function as a constructor to construct new objects and connect the prototypes and you can call function with some numbers. And you can do all the things with one object. You can call it without new, you can call it with new. You can react different ways.
You can fight it, or you can embrace it.
Choice is yours.
All the shit you’ve called, it forgives you. All your hate, it forgives you.
All your libraries, it forgives you.
All your angular and reacts.
All other ways of destroying the language and bend it to your will.
It forgives you everything.
You still can code.
You can get away with anything.
It forgives your incompetence.
“I can go find a job now.” (laughter) It forgives everything.
Even it forgives you when you miss your semicolons. I am not.
Is it a good thing? Is it a bad thing? I don’t know.
But, it’s how the language is written.
And it’s our job to, I believe, to look into it and try to understand it and predict it and be more like the language. Be more like what it’s trying to teach us.
To get it.
To recognize it and to embrace it instead of trying to make it look like your ex-girlfriend. Don’t.
Another great quote as everything in my presentation, “And those who are seen dancing were thought “to be insane by those who couldn’t hear the music.” I’m not saying the thing I showed today is the way to do things.
I’m not saying that before you was doing wrong, now you know how to do things right, No, I just gave you a couple of notes of music I can hear. Maybe I am insane.
Maybe it’s all bullshit.
Or maybe not.
Or maybe it’s not supposed to be written like that. Maybe it’s not supposed to be written at all. Nobody knows.
This is Friedrich Nietzsche who said this quotation. I like it.
Hear the music.
Be brave and forgive.