Is there a way to reuse the value of the if (...) condition in the base scope?

Why

I am joining in compact code and new language features. So I am trying to find a way to reuse the value of the condition statement that opened the current scope without storing it in advance as a variable in the parent scope where it is no longer used.

This will make my code more compact, but easier to read, which is quite subjective. There are probably several approaches to this, so all sorts of solutions are welcome (ES8 preferred, this is not for production use).

Clarification:

The following function is cpu-intensive

fetchData()      // returns [object Object] or false on error

      

It can also be an input stream, whose output will be different on every function call or key-value pair lookup, which must happen thousands of times in a short period of time, but can only happen synchronously / blocking.

My current approach:

I need to enter data 3 times and pollute the global scope. Also, when debugging with breakpoints, the variable was not found in the smallest possible scope.

let data = fetchData()

if (data)
  useData(data)
else
  error()

      

Bad solution:

The data variable still occurs 3 times, pollutes the parent area and you don't know what it means yet when it is declared. The single equal sign (=) could be misinterpreted by the reader or corrected by someone by mistake.

let data
if (data = fetchData())
  useData(data)

      

Lazy way:

if (fetchData())
  use(fetchData())

      

What I want:

'let' avoids interpreting it as the == conditional operator:

if (let data = fetchData()) { // data is declared in the child scope
  parseData(data)
} else {
  console.error('meh')
}

      

Result: SyntaxError: Unexpected identifier

or

if (fetchData()) {
  parseData(fi)
}

      

or

// some kind of symbol, i know this is xor, but it seemed appropriate
if (long.object.key.value().chain.data)
  return JSON.parse(^)

      

or

if (fetchData()) {
  parseData(condition.value)   // or .pop() or just 'condition'
}

      


I just feel like it’s information overload when I think back to my current approach, even if it has its advantages. I believe this can improve readability in situations where the code is really tight or where the function / variable names are so obvious that they almost constitute a sentence.

+3


source to share


2 answers


No, there is no such syntax. The path to work is either the block scale

{
  const data = fetchData()
  if (data)
    useData(data)
  else
    error()
}

      

or IIFE



(function(data) {
  if (data)
    useData(data)
  else
    error()
}(fetchData()));

      

which you can of course do into a reusable function:

function If(predicate, then, other) {
    return function(value) {
        if (predicate) then(value) else other(value)
    };
}
If(Boolean, useData, error)(fetchData());

      

+2


source


Here's a way to do it without any declared variable:

[fetchData()].filter(Boolean).map(useData);

      

It's just for fun, I would not recommend actually using it as it is neither efficient nor very clear code.



Note. If you also want the output meh

... next:

[fetchData()].filter(Boolean).map(useData).length || console.log('meh');

      

+3


source







All Articles