Assigning variable values "on the fly" within expressions

AI Thread 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,031
Reaction score
769
TL;DR Summary
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top