Expressions and their side effects

T

trubar a

Hi

I realize my question may be a bit stupid and perhaps completely
meaningless, but I’m new to programming and thus can’t be sure whether
understanding what the author really meant is essential to my
understanding of certain programming concepts

The book from which I’m learning C# has this to say about expressions
and their side effects:

“ Expressions return values, but they can also have side effects.

• A side effect is an action that affects the state of the program.
• Many expressions are evaluated only for their side effects.

You can create a statement from an expression by placing a statement
terminator (semicolon)
after it. Any value returned by the expression is discarded. For
example, the following
code shows an expression statement. It consists of the assignment
expression (an assignment
operator and two operands) followed by a semicolon. This does the
following two things:

• The expression assigns the value on the right of the operator to the
memory location referenced
by variable x. Although this is probably the main reason for the
statement, this
is considered the side effect.
• After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored. ”


a) If I understand correctly the above quote, then what author states
is that if we have a statement like ‘x = 10;’ then an expression is
the whole statement and not just the part on the right side of an
assignment operator?


b) If the whole statement is considered an expression, then why would
assignment be considered a side effect and not the “main effect”?

“If an assignment of value ‘10’ to variable ‘x’ is a side effect,then
what is the primary purpose ( aka main effect ) of this expression?”


c) “After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored.”

I assume what returns is not the whole expression ‘x = 10’, since in
that case ‘x’ would again be assigned a number? So I assume the
expression has by now been “transformed” to a single value of x ( thus
now expression no longer contains ‘=10’ )?



Thank you
 
S

Scott M.

Hi

I realize my question may be a bit stupid and perhaps completely
meaningless, but I’m new to programming and thus can’t be sure whether
understanding what the author really meant is essential to my
understanding of certain programming concepts

The book from which I’m learning C# has this to say about expressions
and their side effects:

“ Expressions return values, but they can also have side effects.

• A side effect is an action that affects the state of the program.
• Many expressions are evaluated only for their side effects.

You can create a statement from an expression by placing a statement
terminator (semicolon)
after it. Any value returned by the expression is discarded. For
example, the following
code shows an expression statement. It consists of the assignment
expression (an assignment
operator and two operands) followed by a semicolon. This does the
following two things:

• The expression assigns the value on the right of the operator to the
memory location referenced
by variable x. Although this is probably the main reason for the
statement, this
is considered the side effect.
• After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored. ”


a) If I understand correctly the above quote, then what author states
is that if we have a statement like ‘x = 10;’ then an expression is
the whole statement and not just the part on the right side of an
assignment operator?

Yes.


b) If the whole statement is considered an expression, then why would
assignment be considered a side effect and not the “main effect”?

“If an assignment of value ‘10’ to variable ‘x’ is a side effect, then
what is the primary purpose ( aka main effect ) of this expression?”

You're getting a little too caught up in the term "side" effect, which is
not meant to imply importance, rather (additional consequence). In this x
=10; scenario, yes the main point of the expression is to set x to 10.

c) “After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored.”

I assume what returns is not the whole expression ‘x = 10’, since in
that case ‘x’ would again be assigned a number? So I assume the
expression has by now been “transformed” to a single value of x ( thus
now expression no longer contains ‘=10’ )?

Tough to say, but I belive the expression would actually the Boolean true
value.

What I think this text that you are reading is trying to get you to realize
is that sometimes, particularly with method calls, the method will perform
its task, which may cause side-effects, the method may also return from
those tasks with a result or a "return value".

-Scott
 
P

Peter Duniho

trubar said:
Hi

I realize my question may be a bit stupid and perhaps completely
meaningless, but I’m new to programming and thus can’t be sure whether
understanding what the author really meant is essential to my
understanding of certain programming concepts

Understanding the difference between an expression and a side-effect is
IMHO a very important concept in programming. Surely asking about it
and trying to understand it better is neither stupid nor meaningless!
[...] For
example, the following
code shows an expression statement. It consists of the assignment
expression (an assignment
operator and two operands) followed by a semicolon. This does the
following two things:

• The expression assigns the value on the right of the operator to the
memory location referenced
by variable x. Although this is probably the main reason for the
statement, this
is considered the side effect.
• After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored. ”


a) If I understand correctly the above quote, then what author states
is that if we have a statement like ‘x = 10;’ then an expression is
the whole statement and not just the part on the right side of an
assignment operator?

Not quite. He's saying that "x = 10" is an expression, while "x = 10;"
is a statement, made from an expression.

Liberally, even the "10" is an expression. Expressions can be composed
from other expressions. But I would go ahead and just say "10" is a
literal, and note that the decomposition of expressions always results
in some sequence of literals or variables combined with various logic,
such as operators or even method calls.
b) If the whole statement is considered an expression, then why would
assignment be considered a side effect and not the “main effect”?

Because "main effect" is not a well-defined computer science term, while
"side-effect" is. In particular, "side-effect" describes any persistent
change in program state that occurs as the result of executing code.

http://en.wikipedia.org/wiki/Side_effect_(computer_science)

Note that with that understanding of the term "side-effect", the whole
point of most computer programs is their side-effects. That is, a lot
of code can be written without side-effects (and in fact, this is a good
technique to strive for in many cases…functional languages such as Lisp,
XSLT, F#, etc. tend to emphasize that inherently in the language), but
without _any_ side-effects the user of the program would never be able
to observe the program doing anything.
“If an assignment of value ‘10’ to variable ‘x’ is a side effect, then
what is the primary purpose ( aka main effect ) of this expression?”

In that particular example, the purpose of the expression _is_ in fact
its side-effect. The result of the expression (expressions always have
a result) is simply thrown away. It's the persistent change in program
state that is the purpose in writing the expression and making it into a
statement.

But to suggest that the phrase "main effect" should be used as
synonymous with "primary purpose", and then to conclude that "primary
purpose" is somehow mutually exclusive or inconsistent with
"side-effect" is faulty logic. To do so would use arbitrarily chosen
definitions of the terms that are themselves not consistent with
accepted computer science definitions (and of course, if one decides to
choose their own definitions for terms, one can prove _any_
inconsistency they want :) ).
c) “After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored.”

I assume what returns is not the whole expression ‘x = 10’, since in
that case ‘x’ would again be assigned a number? So I assume the
expression has by now been “transformed” to a single value of x ( thus
now expression no longer contains ‘=10’ )?

You're correct that the result of the expression "x = 10" is not in fact
"x = 10". The result of an expression is a value. So "x = 10" cannot
be the result of the expression (*).

But, the result is also not "x". The result of the expression in that
case is "10", regardless of the value in "x". The fact that it happens
to be the same value stored into "x" is almost coincidence, not counting
the fact that the side-effect of interest is in fact assignment of that
value.

That is, while it's true that the result of the expression is the same
value assigned to the variable "x", the interesting part about the
_expression_ is the outcome of the evaluation of what's on the right
side of the assignment operator "=". The fact that the outcome of that
evaluation happens to be assigned to the variable "x" is "just" a
side-effect.

Pete

(*) (Of course, as it turns out there are in fact expressions that can
return other expressions. Again, this is more heavily emphasized in
functional languages, but even in C# this can happen, as in one use of
lambda expressions. But for now, you should just ignore that and
understand that when you write "x = 10", that's an expression, and it
returns a value, rather than itself).
 
H

Helmut Giese

Hi,
c) “After setting the value of x, the expression returns with the new
value of x. But there is
nothing to receive this return value, so it is ignored.”
to clarify: The expression 'x = 10' returns the value 10. It is, in
fact, not used here but it could be.

Think of 'x = y = z = 10'
It's evaluated from right to left so
- the part 'z = 10' is evaluated first and returns 10,
- we now have 'y = 10' which returns 10, too,
- which is used to set x

The last operation also returns 10, but now there is nothing to assign
it to and so it is ignored.
HTH
Helmut Giese
 
T

trubar a

hi

a)
The result of an expression is a value. So "x = 10" cannot
be the result of the expression (*).

But, the result is also not "x". The result of the expression in that
case is "10", regardless of the value in "x".

The result of the expression (expressions always have
a result) is simply thrown away

The result of an expression is a value.

I assume that in math expressions are interpreted a bit differently?
In other words, in math we would consider a variable x being assigned
a value of "10" as being the main effect of the expression "x=10"?!

b)
But, the result is also not "x". The result of the expression in that
case is "10", regardless of the value in "x"

The result of an expression is a value.

I also assume that in math the result of the expression “x=10”
wouldn’t be a value of “10” ( in other words, in math our expression
doesn’t “return” any values ) ? Thus, in math the result of expression
would either be x getting assigned a value or we could perhaps even
claim that our expression doesn’t have a result?!

c)
understand that when you write "x = 10", that's an expression, and it
returns a value, rather than itself).

So returned value (10) is no longer an expression, but just its
result?


thank you
 
P

Peter Duniho

trubar said:
I assume that in math expressions are interpreted a bit differently?
In other words, in math we would consider a variable x being assigned
a value of "10" as being the main effect of the expression "x=10"?!

You can "consider" it as whatever you like. There is no concept of
"side-effect" in traditional math (though of course, mathematics
applying to algorithms would tend to use the same definition as found in
computer science), so how you describe the algebraic formula in math has
no relevance to the broader question.

I will point out, however, that in mathematics the = symbol is not an
assignment operator. It's a description of an _existing_ relationship
between the two sides of the formula. There's no "effect" at all, main
or otherwise.
b)

I also assume that in math the result of the expression “x=10”
wouldn’t be a value of “10” ( in other words, in math our expression
doesn’t “return” any values ) ? Thus, in math the result of expression
would either be x getting assigned a value or we could perhaps even
claim that our expression doesn’t have a result?!

See above. "In math", the question you are asking doesn't even make sense.
c)

So returned value (10) is no longer an expression, but just its
result?

In programming, expressions are what you write in code. They are part f
the static program description. The value returned by an expression is
something else entirely. It's what an expression in code causes to
happen when the code is actually executed.

So, yes…it's trivially true that the "returned value is no longer an
expression", since the expression only has a value at execution time as
a consequence of evaluating it.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top