So this revelation of sorts was presented to me during a code review. "Why did you use
let here? I don't see it changing anywhere in this function?"
Well you see, I began to explain, nearly every es6 tutorial I've seen declares
let as the new de-facto
var (with block scoping). Grab
let as your go-to variable unless you know for a fact that you don't need to re-assign it - in that case you can use
const. This seemed like a good approach to me, keeps my variables flexible until I choose to explicitly restrict them.
"No, no, no" he began. "You should reach for
const by default, and only if you intend for the variable to change should you use
let. As soon as I see a
let I start to look for where it will be reassigned. If you use
const I can simply forget about it and have one less thing to think about."
This made so much sense to me that I'm kinda surprised it's not the default recommendation. This approach also fits right in with the functional/declarative approach to programming that I'm trying to adopt as it clearly states intent and minimizes distractions. It may seem like a minor change, but in more complex functions with multiple arguments and reassignments, this small change can be very helpful in reducing the cognitive load of the function.
Now instead of mindlessly using
let 'just in case' I may want to re-assign later, I just grab
const by default and in the event I'd like to re-assign halfway through a function, I'll go back and change it to a
Thanks for reading my first post on dev.to!