I very much like knowing where a local variable stops being relevant. Often I'll do things like
```ts
let foo;
{
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.

@afontaine do you think I’m being too silly and pre-optimizing by not wanting an IIFE for perf reasons? It also seems so much ceremony just to hide a few temporary variables from subsequent code 😕? I’d worry my kouhai would see such code and be more alarmed and wary, wondering what dread cause moved me to write something like that, and they’ll be peeved it was nothing more than containing intermediate variable.

But that is of course a solution.

@22

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:

github.com/fasiha/texshade-py/

`slices` is no longer used by this line and I wish it was more obvious. (Python is even more the pits than JavaScript in its lack of block scope syntax.)

Follow

@22

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)

Sign in to participate in the conversation
Mastodon

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!