1 CS 410 Mastery in Programming Chapter 4 Side Effects Herbert G. Mayer, PSU CS status 7/9/2011
2 Syllabus Side Effects Side Effects Language and Side Effect Language and Side Effect Sample Side Effect Sample Side Effect References References
3 Side Effects Side Effects in an executing program are: Actions performed by a function Changing the program state In a way that is generally surprising Due to their unnatural place, or strange time! As a consequence, side effects are hard to track, their impact difficult to account for, and programs generating them are hard to maintain What Side Effects are NOT! These changes are not caused by agents other than the programmer You/the programmer put them there for a reason Side effects are not bad per se!
4 Side Effects Why are they practiced? For “convenience” lumped together with the action proper of a function For efficiency, to avoid invoking another, separate function Fair to say: They are short-cuts How to avoid Side Effects? Assuming the impact of the Side Effect is needed Separate it out in an imperative language, thus simulating the behaviour/constraint of a functional language Why avoid Side Effects? A program written with Side Effects, no matter how well designed, is more difficult to read --harder to maintain– than a program without Side Effects But watch out for ‘snake-oil” propaganda : The work of the Side Effect has to be accomplished somehow
5 Language and Side Effect Imperative languages (e.g. C++) generally encourage side effects Underlying compute model is Turing Machine Side Effect means: a change in machine/program state Enabled by references to, and changes of, global objects Can change globals directly, or indirectly via reference parameters or pointers Can change files, state, condition codes, subtle state Other languages prone to side-effects: Fortran, PL/I, Pascal, Ada, C
6 Language and Side Effect Can be worse in languages other than C++ More complex languages with nested procedural scope offer even more chances for side effects E.g. PL/I, Ada, Pascal, Modula-2, Algol-60, Algol68, … Functional languages Underlying compute model generally is Lambda Calculus Eliminates/reduces state change of program/machine E.g. Haskell, ML, Prolog, …Caveat Watch out for PR! The language proponents generally have an agenda, maybe even a noble one. But watch out Functional language are not inherently better, just because they reduced the chances for side-effects Imperative languages with nested procedural scope are not inherently better, just because they allow better data sharing
7 Sample Side Effect #include #include #define MAX 10// arbitrary array bound int global_i = 5;// arbitrary number within array bounds int arr[ ] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }; int one()// bad function with side-effect { // one global_i++; return 1; } //end one void print() { // print for ( int i = 0; i < MAX; i++ ) { printf( "arr[%d] = %2d\n", i, arr[ i ] ); } //end for } //end print int main() { // main arr[ one() + global_i ] = arr[ one() + global_i ]; print(); } //end main
8 Sample Side Effect, Output arr[0] = 0 arr[1] = 1 arr[2] = 4 arr[3] = 9 arr[4] = 16 arr[5] = 25 arr[6] = 36 arr[7] = 64 element arr[ 7 ] overridden with arr[ 8 ] arr[8] = 64 arr[9] = 81
9 Sample Side Effect, Output arr[ one() + global_i ] = arr[ one() + global_i ] = arr[ one() + global_i ]; For the following assignment, what is the output:
10 Sample Side Effect, Output arr[0] = 0 arr[1] = 1 arr[2] = 4 arr[3] = 9 arr[4] = 16 arr[5] = 25 arr[6] = 36 arr[7] = 81 element arr[ 7 ] overridden with arr[ 9 ] arr[8] = 81 element arr[ 8 ] overridden with arr[ 9 ] arr[9] = 81
11 Sample Side Effect Was the correct element overridden? Was the right element referenced? Which language rule should apply? Must left-hand side of assignment operator = be evaluated first? Or the right-hand side? Should the function main() be allowed to change global_i or any other global? Did the programmer do something wrong?
12 References Side effect: cience) Functional language: nguages_by_category#Functional_languages Turing Machine: Functional vs Imperative: us/library/bb aspx