Assigning variable values "on the fly" within expressions

In summary, "Assignment expression" is a term sometimes used to describe the syntax used in the conversation, where variables are assigned values within an expression. This can be a debated topic in terms of whether it is a good practice or not, as it can obscure code and potentially lead to errors. The order of evaluation in this syntax is important and should be understood according to the language specification. This syntax is also supported in C++ and APL, but caution should be taken when using it.
  • #1
Swamp Thing
Insights Author
908
574
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
  • #2
“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
  • #3
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
 
  • #4
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
  • #5
Nugatory said:
Be very sure that you understand what your language specification says about order of evaluation.
Please forgive me for paraphrasing that liberally!
 
  • Haha
Likes Nugatory
  • #6
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
 

What is meant by "assigning variable values on the fly" within expressions?

Assigning variable values on the fly within expressions refers to the practice of setting the value of a variable directly within an expression, without explicitly declaring it beforehand. This allows for more concise and dynamic code.

Why would someone want to assign variable values on the fly within expressions?

Assigning variable values on the fly within expressions can be useful for situations where the value of a variable may change frequently or where there is a need for a more efficient and concise code. It also allows for more flexibility in programming and can make the code easier to read and understand.

What are the potential risks of assigning variable values on the fly within expressions?

Some potential risks of assigning variable values on the fly within expressions include the potential for errors and bugs, as well as a lack of clarity and readability in the code. It can also make debugging more difficult if there are issues with the assigned values.

Are there any best practices for assigning variable values on the fly within expressions?

Yes, it is important to use clear and concise variable names and to ensure that the assigned values are appropriate for the intended use. It is also recommended to use this technique sparingly and to avoid nesting multiple assignments within one expression to maintain readability.

How does assigning variable values on the fly within expressions differ from traditional variable assignment?

Traditional variable assignment involves declaring a variable and assigning a value to it before using it in an expression. Assigning variable values on the fly within expressions, on the other hand, allows for the direct setting of a value within the expression itself. This can save time and space in the code, but may also increase the risk of errors and make the code less readable.

Similar threads

  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
4
Views
345
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
269
  • Programming and Computer Science
Replies
2
Views
904
  • Programming and Computer Science
Replies
1
Views
1K
Replies
80
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
649
Back
Top