A Compact Ternary/Conditional Operator, Write-in-place Nullish Coalescence Operator!

Compact Ternary/Conditional Operator

Can the specification include a more compact, write-in-place conditional assignment operator, similar to the Nullish coalescing operator, but with an in-place write?

// Current
( condition ) ? run this code : run this code instead ;

//New
( condition ) ?? run this code [ : run this code instead ] ;

Example

let fileNAME = 'FILENAME'

// Current
fileNAME += boolFileModified ? '*' : '' ; // 'FILENAME*'

// New
boolFileModified ?? fileNAME + '*' ; // 'FILENAME*'

// New (double-write!)
fileNAME = boolFileModified ?? fileNAME + '*' ; // 'FILENAME*'

The benefit of an in-place Ternary/Conditional, Nullish coalescing assignment operator, is the evaluation is write-in-place, which inherently allows its evaluation of to be conditional.

let result = []

// Current
myArray.map((u,i,me)=>{
    // Perform this once, but do it first!
    // Wasteful, comparison made, each iteration, 
    // unless we employ counter-intuitive logic, 
    // which can be error-prone
	if( i == 0 )
	{
	  result.push('Title: Array of initialized values')
	  result.push(u)
	}
	else if ( i > 1 && i < me.length-1)
	{
	  result.push(u)
	}
	else
	{
	  result.push(u)
	  result.push('Coda: Array of initialized values completed!')
	}
	return u
})

// New
myArray.map((u,i,me)=>{
	// Perform this once, but do it first!
	( i == 0 ) ?? result.push('Title: Array of initialized values') ;
	result.push(u) ;
	( i == me.length-1 ) ?? result.push('Coda: Array of initialized values completed!') ;
	return u
})

The new Ternary/Conditional could also be scope-able!

// New
myArray.map((u,i,me)=>{
	// Perform this once, but do it first!
	( i == 0 ) ?? 
	{
	  result.push('Title: Array of initialized values')
	  result.push(u)
	}
	:
	{
	  result.push({ index:i, value:u })
	}
	return u
})

A current approach to improve performance, is to evaluate the interrogative statements outside the loop

let result = []

// Current (Optmized) version 1
result.push('Title: Array of initialized values')
result.push(myArray[0]) // error-prone if myArray not properly initialized
for(let i = 1; i < myArray.length-1; i++)
{
  result.push(u)
}
result.push(myArray[10]) // error-prone, if myArray not bounds-checked
result.push('Coda: Array of initialized values completed!')

Or implement a state-machine, to re-factor the execution trace, at runtime, to ensure its runtime performance is always optimal.

// Current (Optmized) version 2
let k = 0
let result = []
let execution_state = [0,1,2]
let execution_action = 
{
  0:() => { result.push('Title: Array of initialized values') ; result.push(u) ; k++ ; }
  1:() => { result.push(u) ; k += ( i == me.length-1 ) ? 1 : 0 ; }
  2:() => { result.push(u) ; result.push('Coda: Array of initialized values completed!') ; }
}
myArray.map((u,i,me)=>{
	execution_action[k](u)
	return u
})

However, an in-place conditional-write bests these previous approaches, in runtime performance, because the CPU only evaluates the statement, if true -- because it manages the state-machine, internally.

// New (Optimized)
myArray.map((u,i,me)=>{
	// Perform this once, but do it first!
	( i == 0 ) ?? result.push('Title: Array of initialized values') ;
	result.push(u) ;
	( i == me.length-1 ) ?? result.push('Coda: Array of initialized values completed!') ;
	return u
})

Your suggestion is less compact, and can already be achieved with condition && runThisCode() or if (condition) { runThisCode(); }.

2 Likes

I like how you implemented a state machine without switch/case, but it could've been more concise if you used an array literal, to avoid explicit indices.

About the proposal, it's very comprehensive!, but I'm not sure it's a good idea. I agree with @ljharb