Learn how to stop being surprised by your code
The bug that changed everything
I don’t remember the exact reason why dateString evaluated to an undefined value. But, we can imagine that I tried to access the field of an Object on which that field did not exist. Doing so would result in an undefined value.
The code snippets show that the moment function will accept the undefined argument and return a valid Moment object of the current date.
At the very least, I expected that passing undefined to the moment function would result in an invalid Moment object. Instead, I got silent approval and a failure waiting to happen.
new Date(Date.now()) expression to the current date.
Before continuing, let us take a look at how I’m guessing many of us learn programming languages. This is complete speculation base of my own experience learning programming languages and the experiences of a few others whose learning experiences I have some insight into.
How we learn programming languages
Although we each have our different approaches to learning programming languages, I suspect there are some common elements in our approaches.
Perhaps we begin by learning the basic syntax, grammar, and key constructs of the language, then we try to use the language to do something relatively simple. If we are not first-time language learners, we attempt to do things we have done using programming languages with which we are more proficient.
Furthermore, after we’ve understood the basic syntax, grammar, and key constructs of the language, our intuitions set in. We begin to use the language with confidence, knowing that we can rely on information sources such as Google and Stack Overflow for anything we missed.
When we encounter something unexpected, we will do a bit of research, understand what’s going on, fix whatever issue we had, and tuck away that knowledge, perhaps never to be surprised by it again.
But, what if we had spent more time with the fundamentals of the language, experimenting with its peculiarities, learning the language more deeply, and reveling in the learning process?
No more surprises
Whatever programming language(s) you have chosen to master, there is a definitive document somewhere that describes its rules of operation. Find it and study it. In my case, I spend time on the ECMAScript® Language Specification and the Ruby Docs.
In truth, we’ll never stop having surprises. There’s too much information for us to take in and process. But, we can do our best to get the knowledge we need to be surprised less often. We should take each surprise as an opportunity to go deeper.
To dismiss a strange-at-first-glance behavior in our tools as something weird and not trying to understand why things operate the way they do is to choose to remain ignorant. And, that is unacceptable (I’m talking to myself here.)
As we wield our tools to build systems that people rely on, we have a responsibility to wield them with mastery and with care.
- Thank you, Code2040, for gifting me a subscription to Frontend Masters.
- Kyle, you are an amazing teacher. Thanks for sharing, and keep up the good work.
- To the moment.js developers and contributors, you have created a tool that makes it easy to work with dates. Thank you.
- [Free] ECMAScript® Language Specification
- [Free + Optional Paid Version] You Don’t Know JS
- [Paid] Frontend Masters