4 ms·
The try and catch inside the async function is also great. Curious though, is using const over let common? I usually use const for imports and module level glob
by viperscape 10y ago
The try and catch inside the async function is also great. Curious though, is using const over let common? I usually use const for imports and module level globals, and let inside functions. I recently got back into javascript, and it's nice to see it flourishing.
- bb85 10y agoThe great thing about let and const, for me, is that it gives you crucial information about variables without having to look further down. const should be the default, and if you're going to reassign it, then use let. In most code, you'll use const on the vast majority of variables, and it'll make let assignments stick out, which helps a lot when you're browsing the code or refactoring.
- spraak 10y agoThis right here. One of my friends (a more experienced developer) told me at coffee one day, "I use const for everything" which really stuck with me. It really helps make it clear if you need a mutation to have let stand out.
- rpastuszak 10y agoHowever, it's worth noting that using const doesn't make variables immutable. It only protects them of being reassigned: const val = 'a'; val = 'b'; // TypeError const obj = { val: 'a' }; obj.val = 'b'; // This line will still work. obj = { val: 'b' }; // TypeError
- dcherman 10y agoYep. In those cases, I still prefer let personally even though I know the variable itself will not be re-assigned, kind of as an indicator that I expect the referenced object to be mutated.
- candu 10y agoComing from a background that includes some experience with C / C++, `const` here works as I expect it to. I'd prefer `const` even for the object reference case: I get errors on reassignment this way, and presumably it makes additional optimizations possible. To each their own, I suppose :) There's also `Object.defineProperty()`, `Object.freeze()` for more control over mutability.
- WorldMaker 10y agoI just wish let was in fact const (and something like "local" could be let), simply because the keyword let is nicer to type, easier to read, and more importantly better in line with the meaning of let in Lisp/Scheme/ML/OCaml/et al.
- spraak 10y agoYeah, I was thinking the same! I was even considering writing a custom babel transform to do this for me :P
- g_delgado14 10y agoconst is more declarative as you can immediately tell that the data isn't going to change throughout the duration of the program (although technically you can change the data, just not the reference).
- lewisl9029 10y ago> although technically you can change the data, just not the reference This is why I'm hoping to see native JS immutable data structures eventually (maybe it's already in a proposal somewhere?). ImmutableJS and Mori are great, but having a native solution that's available everywhere would be ideal.
- callahad 10y agoConsider looking at other languages. ClojureScript does a pretty good job at immutability, Elm is great for immutability + strong typing, and PureScript adds in Haskell's advanced type system (typeclasses, HKTs, etc.)
- lewisl9029 10y agoDefinitely. ClojureScript is what I usually choose for my own projects, but the reality of the front-end job market is that JavaScript still dominates, so improvements to JavaScript itself will still have a significant impact on the lives of developers everywhere regardless of the existence of other compile-to-JS languages.
- shimn 10y agoWhen I want to ensure strict immutability, I use an implementation of deepFreeze (https://github.com/jsdf/deep-freeze https://github.com/jsdf/deep-freeze) that recursively calls Object.freeze() on all child properties. Obviously slower than strict browser support. Does ClojureScript rely on the Clojure compiler to ensure the properties aren't modified or is there some polyfill?
- WalterSear 10y agoIMHE, const is the generally the default and let is the exception.
- sotojuan 10y agoI'm alone in this but I prefer `let` over everything else unless it's something like Redux action strings, database connections strings, or anything of the like. I rarely use `const` for the ridiculous reason that I find it too long (5 characters!). `let` is short and reads nicely and my programming style in general never mutates stuff anyway (I use Ramda/always return new data). That said, `const` is picking up to be the default for everything. I can definitely see good reasons to use it when declaring functions and required modules or working in a team.
- nemild 10y agoI'm curious why you let the amount of typing dictate what you use? If five characters is such an impediment, why not just use a snippet letting you have less error prone code than currently (e.g., 'co ' expands to const).
- garyclarke27 10y agoAs you admitted, your behaviour is rediculous, hope for your sake, your boss or customers don't read this.
- bubersson 10y agoWe also favor using const as a default for everything in our team's code. When someone uses let, it's right away obvious that the reference will change. One case that surprised me a bit is that it's correct to use const in for-of loop, e.g. for (const a of arr) {}
- spraak 10y agoI've been using a lot of const function declarations `const fn = (i) => ...`
- tlrobinson 10y agoYes, http://eslint.org/docs/rules/prefer-const http://eslint.org/docs/rules/prefer-const is a fairly common [1] ESLint rule. 1. Admittedly my only data point is AirBnB's style guide / ESLint config: https://github.com/airbnb/javascript#references--prefer-const https://github.com/airbnb/javascript#references--prefer-cons...