I very much like knowing where a local variable stops being relevant. Often I'll do things like
const bar = f();
const baz = g();
foo = h(bar, baz);
to contain intermediate variables in a lexical scope (TypeScript is trashy that I have to define `foo` outside the braces—Julia etc. blocks can return).
I wish syntax or editors helped even more in showing when a variable stops mattering. It might be one of those small ergonomic things that add up to much more.
@22 this might just be because it's a trivial example, but I'd generally write a "block that returns" as a function in TS. Then, `foo` could even be a `const`, depending.
I think there might be a couple good options depending on complexity...
If you don't need to check `bar` or `baz` for errors, etc. inlining here might not be the worst? It can be spaced out in a way that is still _mostly_ readable
const foo = `h(f(), g())`
but at the same time, if you suddenly need to check `bar` and/or `baz` and sanitize values? then your function is hiding temporary variables _and_ error checking logic, which I think is definitely a win
@afontaine right, in my example f and g and h are standins for expressions of arbitrary complexity. The example that prompted the toot:
Yeah I think there's a line here where encapsulating that complexity in a separate function makes a lot of sense. That link is kind of difficult to read (but that might be more of a lack of domain knowledge on my part)
Server run by the main developers of the project It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!