Assigning variable values "on the fly" within expressions

Click For Summary
The discussion centers around the use of assignment expressions in programming languages like JavaScript, Mathematica, and C++. The syntax in question allows for variable assignments within expressions, exemplified by the equation z = ((a=3) +( b=2) )**2 + 1/a + 1/b. While this syntax is recognized, particularly as an "assignment expression," its use raises concerns regarding code clarity and potential errors. The conversation highlights that while the expression works in C++, the order of evaluation is not guaranteed, which can lead to unpredictable behavior. The importance of understanding language specifications regarding evaluation order is emphasized, as it can affect the correctness of the code. Additionally, a mention of APL illustrates that variable assignments can be managed differently, allowing for clearer initialization within expressions. Overall, the discussion warns against the risks of using assignment expressions without a thorough understanding of their implications.
Swamp Thing
Insights Author
Messages
1,045
Reaction score
775
TL;DR
This works in JavaScript : ((a=3) +( b=2) )**2 + 1/a +1/b

Is there a term for it? Does it work in may other languages?
I've used this kind of thing often in JavaScript and Mathematica.
Code:
z = ((a=3) +( b=2) )**2 + 1/a +1/b
I'm currently learning Julia, and it doesn't seem to be supported.

What is this syntax called?
 
Technology news on Phys.org
“Assignment expression” is one term sometimes used.

I won’t enter into the debate about whether using assignment expressions is a good idea (basically, whether the unnecessary and irrelevant reduction in line count justifies the gratuitous obscuration of the code, interference with the compiler’s ability to optimize, and potential for introducing subtle errors) but I will say that if you’re going to use them…. Be very sure that you understand what your language specification says about order of evaluation.
 
  • Like
  • Haha
Likes Swamp Thing, jedishrfu, pbuk and 2 others
It works in C++, too.

Code:
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int a, b;
    int c = ((a = 3) + (b = 2)) * 2;  
    cout << a << ' ' << b << ' ' << c << endl;
    return 0;
}

produces the output

Code:
3 2 10
 
jtbell said:
It works in C++, too.
But note that the more complicated expression in the OP (((a = 3) + (b = 2)) ** 2 + 1/a + 1/b) will compile but there is no guarantee that the compiler will set a to 3 before it calculates 1/a (the order of these operations is not defined in the standard).

So as somebody not quite said upthread, even if this works it is never a good idea.
 
Last edited:
  • Like
Likes phyzguy, Nugatory, Swamp Thing and 1 other person
Nugatory said:
Be very sure that you understand what your language specification says about order of evaluation.
Please forgive me for paraphrasing that liberally!
 
In APL, variable assignment can be done according to the order of evaluation. In this example, the right Z is declared, but uninitialized, where initialization occurs within the parenthesis. The expression is 2 modulo (Z##\leftarrow## 1 2 3 4 5) reduce Z (delete all values where Z modulo 2 are zero).

$$ 2|(Z \leftarrow 1 \ 2 \ 3 \ 4 \ 5)/Z $$

returns 1 3 5
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
235
Views
14K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
4
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
8K