cs 201 computer systems programming chapter 9 “ side effects ”

13
1 CS 201 Computer Systems Programming Chapter 9 “Side Effects” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS Status 7/15/2014 Status 7/15/2014

Upload: serina-shepard

Post on 02-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Herbert G. Mayer, PSU CS Status 7/15/2014. CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”. Syllabus. Side Effects Language and Side Effect Sample Side Effect References. System programmer must be keenly aware of side-effects, particularly in Imperative Languages. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

1

CS 201Computer Systems Programming

Chapter 9“Side Effects”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 7/15/2014Status 7/15/2014

Page 2: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

2

Syllabus Side EffectsSide Effects

Language and Side EffectLanguage and Side Effect

Sample Side EffectSample Side Effect

ReferencesReferences

Page 3: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

3

Side EffectsSide EffectsSide Effects in imperative programs are: in imperative programs are:

Actions performed by a function Changing the program state In a way that is generally surprising Due to their unexpected, even unnatural, location or 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!What Side Effects are NOT! These unexpected changes are not caused by mystery

agents; instead they are caused by the programmer, by you, who put them there for a reason, sometimes even good reasons

Side effects are not bad per se! Functional languages are designed to be Side Effect-free!

Page 4: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

4

Side EffectsWhy do Side Effects occur? Why are they practiced?Why do Side Effects occur? Why are they practiced?

For convenience lumped together with action proper of a function For efficiency, to avoid invoking another, separate function Fair to say: They are frequently just short-cuts

How to avoid Side Effects?How to avoid Side Effects? Either use a functional language, or Assuming the impact of the Side Effect is needed: Separate the Side Effect out in an imperative language, thus

simulating the behaviour/constraint of a functional language

Why avoid Side Effects?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 programmatic result of the Side Effect has to be accomplished somehow

Page 5: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

5

Language and Side Effect

Imperative languages (e.g. C++) generally encourage Imperative languages (e.g. C++) generally encourage side effectsside 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 Imperative languages can change globals directly, or

indirectly via reference parameters or pointers Can change files, state, condition code, machine resource Languages prone to side effects include: Fortran, PL/I,

Pascal, Ada, C, C++

Page 6: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

6

Language and Side EffectCan be worse (more obscure) in languages other than C/C++Can be worse (more obscure) in languages other than C/C++

More complex languages with nested procedural scope offer even more chances for side effects; C has no lexically nested functions

But see other languages, e.g. PL/I, Ada, Pascal, Modula-2, Algol-68 …

Functional languagesFunctional languages Underlying compute model generally is Lambda Calculus One goal of functional languages is to eliminate side effect E.g. Haskell, ML, Prolog, …

CaveatCaveat Watch out for PR! Language proponents generally have an agenda,

maybe even a noble one, but can stretch the truth in one direction or another without telling you, often without being aware: Never underestimate the power of self-deception!

Functional language are not inherently better, just because they reduce chances for side effects; they are different and have different shortcomings

Imperative languages with nested procedural scope are not inherently better, just because they allow better data sharing

Page 7: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

7

Sample1 Side Effect#include <stdio.h>#include <stdio.h>#define MAX 10#define MAX 10 // arbitrary array bound for sample// arbitrary array bound for sample

int global_i = 5;int global_i = 5; // arbitrary number within array bounds// arbitrary number within array boundsint arr[ ] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };int arr[ ] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };

int one()int one() // // suspectsuspect function with side effect function with side effect{ // one{ // one

global_i++;global_i++;return 1;return 1;

} //end one} //end one

void print()void print(){ // print{ // print

for ( int i = 0; i < MAX; i++ ) {for ( int i = 0; i < MAX; i++ ) {printf( "arr[%d] = %2d\n", i, arr[ i ] );printf( "arr[%d] = %2d\n", i, arr[ i ] );

} //end for} //end for} //end print} //end print

int main()int main(){ // main{ // main

arr[ one() + global_i ] = arr[ one() + global_i ];arr[ one() + global_i ] = arr[ one() + global_i ];print();print();

} //end main} //end main

Page 8: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

8

Sample1 Side Effect, Output Start

arr[0] = 0arr[0] = 0arr[1] = 1arr[1] = 1arr[2] = 4arr[2] = 4arr[3] = 9arr[3] = 9arr[4] = 16arr[4] = 16

. . . And more, but what exactly?. . . And more, but what exactly?

Page 9: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

9

Sample1 Side Effect, Output

arr[0] = 0arr[0] = 0arr[1] = 1arr[1] = 1arr[2] = 4arr[2] = 4arr[3] = 9arr[3] = 9arr[4] = 16arr[4] = 16arr[5] = 25arr[5] = 25arr[6] = 36arr[6] = 36arr[7] = 64 arr[7] = 64 element arr[ 7 ] overridden with arr[ 8 ] element arr[ 7 ] overridden with arr[ 8 ]arr[8] = 64arr[8] = 64arr[9] = 81arr[9] = 81

Page 10: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

10

Sample2 Side Effect, Output

// Sample 1 modified: Sample 2:// Sample 1 modified: Sample 2:int main()int main(){ // main{ // main

arr[ one() + global_i ]arr[ one() + global_i ]= arr[ one() + global_i ]= arr[ one() + global_i ]

= arr[ one() + global_i ];= arr[ one() + global_i ];print();print();

} //end main} //end main

For the following modified assignment of sample1, For the following modified assignment of sample1, what will be output?what will be output?

Page 11: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

11

Sample Side Effect, Output

arr[0] = 0arr[0] = 0arr[1] = 1arr[1] = 1arr[2] = 4arr[2] = 4arr[3] = 9arr[3] = 9arr[4] = 16arr[4] = 16arr[5] = 25arr[5] = 25arr[6] = 36arr[6] = 36arr[7] = 81 arr[7] = 81 element arr[ 7 ] overridden with arr[ 9 ] element arr[ 7 ] overridden with arr[ 9 ]arr[8] = 81 arr[8] = 81 element arr[ 8 ] overridden with arr[ 9 ] element arr[ 8 ] overridden with arr[ 9 ]arr[9] = 81arr[9] = 81

Page 12: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

12

Sample Side EffectWas the Was the correctcorrect element overridden? element overridden?

Was the Was the rightright element referenced? element referenced?

Which language rule should apply?Which language rule should apply?

Must left-hand side of assignment operator = be Must left-hand side of assignment operator = be evaluated first? Or the right-hand side? And every evaluated first? Or the right-hand side? And every part of it?part of it?

Should the function Should the function main()main() be allowed to change be allowed to change global_iglobal_i or any other global? or any other global?

Did the programmer do something wrong?Did the programmer do something wrong?

Page 13: CS 201 Computer Systems Programming Chapter 9 “ Side Effects ”

13

References1.1. Side effect: Side effect:

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

2.2. Functional language: Functional language: http://en.wikipedia.org/wiki/List_of_programming_lahttp://en.wikipedia.org/wiki/List_of_programming_languages_by_category#Functional_languagesnguages_by_category#Functional_languages

3.3. Turing Machine: Turing Machine: http://plato.stanford.edu/entries/turing-machine/http://plato.stanford.edu/entries/turing-machine/

4.4. Functional vs Imperative: Functional vs Imperative: http://msdn.microsoft.com/en-us/library/bb669144.ashttp://msdn.microsoft.com/en-us/library/bb669144.aspxpx