in reply to Re^33: Why is the execution order of subexpressions undefined? (magic ruts)
in thread Why is the execution order of subexpressions undefined?

So now the only way to avoid the compiler subverting what the programmer know he needs is for the programmer to play hostage to the compiler and code
Well, either that or avoid side-effects in your functions. Is that really so much to ask? You do realize you should try to minimize the use of side-effects in general, right? Just like you should limit your use of global variables and gotos?
  • Comment on Re^34: Side-effects are bad form [was: Why is the execution order...]

Replies are listed 'Best First'.
Re^35: Side-effects are bad form [was: Why is the execution order...]
by Anonymous Monk on Apr 19, 2005 at 15:32 UTC
    Assignment is a side-effect. And it's hard to write a useful Perl program that doesn't do assignment.
      Yeah, but you can keep the effects *local* and not spread out all over the place like BrowserUk seems to be worried about.
Re^35: Side-effects are bad form [was: Why is the execution order...]
by BrowserUk (Patriarch) on Apr 19, 2005 at 15:31 UTC

    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?
      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.
      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.