in reply to Re^34: Side-effects are bad form [was: Why is the execution order...]
in thread Why is the execution order of subexpressions undefined?

Any form of IO is a side-effect, regardless of where you do it.

In an imperative language, 'functions' (procdures that return values if you prefer) are the major mechanism of code structuring. When I do IO to a database, I call a function (or method (same difference). When I read or write to a file, I call a function. When I read or write to a socket udp/tcp/http/ftp/telnet/et al, it is all done with functions and all of those functions that do IO have side-effects. f(a) + g(b) * h(c)

f(a) retrieves the range from the radar.

g(b) retrieves a correction factor from the wind speed indicator.

h(c) gets a scalar from a lookup table in memory. keyed upon the type of shell (HE, tracer, AP) that will load next from the belt.

The final result is used to adjust the elevation of the barrel in real time.

How do you avoid the side effects?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.
Rule 1 has a caveat! -- Who broke the cabal?
  • Comment on Re^35: Side-effects are bad form [was: Why is the execution order...]
  • Download Code

Replies are listed 'Best First'.
Re^36: Side-effects are bad form [was: Why is the execution order...]
by Anonymous Monk on Apr 19, 2005 at 15:51 UTC
    OK, for all pedants out there, here's a slight modification:
    Avoid side-effects if at all possible. If you absolutely have to use them, make sure you try as hard as you can to have them act in as limited scope as possible.
    Your example is perfect. Why in the world should f interact or depend on g or h? They shouldn't! It is a sign of poor programming if they did. BTW, I (heart) Perl, but God help any unfortunate soul who's life would depend on the correct functioning of a Perl program.
Re^36: Side-effects are bad form [was: Why is the execution order...]
by Anonymous Monk on Apr 19, 2005 at 16:04 UTC
    Trivial.

    my $f = f(a); my $g = g(b); my $h = h(c); $f + $g * $h;
    And then you fire the programmer putting such side-effects in the code.
    A reply falls below the community's threshold of quality. You may see it by logging in.