Destructuring assignments in JavaScript, combined with the ternary operator on object values is quite succinct yet expressive.


const { proxyType, proxyProtocol } = originalProtocol === 'ws' ? { proxyType: 'WebSocket', proxyProtocol: 'wss' } : { proxyType: 'HTTP', proxyProtocol: 'https' }

· · Web · 2 · 1 · 2
@aral Javascript turned into a pretty good language, even if it's got some warts.
@Moon @aral proof that you can polish a turd into something great.

(and that it's better to improve a thing instead of starting from scratch)

@aral maybe a bit less expressive, but way shorter:

const [ proxyType, proxyProtocol ] = originalProtocol === 'ws' ? [ 'WebSocket', 'wss' ] : [ 'HTTP', 'https' ]

@aral @severo_bo
Rust replaces ternary with if statements as expressions, e.g.

let (proxy_type, proxy_protocol) =
if original_protocol == "ws" {
("WebSocket", "ws")
} else {
("HTTP", "https")

You can also do:
let thing = if let Some(t) = maybe_thing {
} else {
warn!("maybe_thing wasn't a thing");
And because of the return (or continue in a loop), thing can be used. Since there is no implicit null and you need to check, I use this construct a lot.

Sign in to participate in the conversation
Aral’s Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!