(And here's the link to it on GitHub).
Notice the code in question:
if (maybeKey !== undefined) {
key = '' + maybeKey
}
It's not hard to understand what this code does. If maybeKey
is not undefined
, we set the key
property to the stringified version of maybeKey
.
The string conversion is a little JS trick -
'' + maybeKey
will convertmaybeKey
to a string. For example'' + 2
returns"2"
.
But here it's all about the why. The comment for this code is great. It calls out the problem, gives two examples and explains the long term plan as well as the short term solution.
If you're after a comment that I left in code I wrote, this comment in some TypeScript => Closure Compiler code is a really good example of the types of comments that I think are super valuable.
All code can eventually be understood; code is ultimately instructions to the computer to do something. Code can be confusing but it can't lie, given enough time any developer can step through code and work out exactly what it does. But it's much harder to work out why it does that. Give your colleagues (or future you, in six months time) the context behind why the code does what it does and you'll be much better for it.