-
Beyond the Mouse – AShort Course on
Programming1. Thinking programs
Ronni Grapenthin
Geophysical Institute, University of AlaskaFairbanks
September 12, 2011 “The Uncomfortable Truths Well”,http://xkcd.com/568 (April 13, 2009)
1 / 21
-
Outline
1 Overview and Philosophies
2 Thinking programs
3 Building programs
4 Summary
2 / 21
-
Outline
1 Overview and Philosophies
2 Thinking programs
3 Building programs
4 Summary
3 / 21
-
The Program . . .
Rethink!NO!Need to
program?START
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Send email with N and comments to Ronni
NO!
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Yes.Interesting?Useful?
Go through exercisesSend email with N and
comments to Ronni
NO!
NO!
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Yes.Interesting?Useful?
Go through exercisesSend email with N and
comments to Ronni
NO!
NO!
Have a l i fe!
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Yes.Interesting?Useful?
Go through exercisesSend email with N and
comments to Ronni
NO!
NO!
Have a l i fe!
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Attend LAB N
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Yes.Interesting?Useful?
Go through exercisesSend email with N and
comments to Ronni
NO!
NO!
Have a l i fe!
N = N+1
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Attend LAB N
Interesting?Useful?
NO! Send email with LAB N and comments to Ronni
Yes.
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Yes.Interesting?Useful?
Go through exercisesSend email with N and
comments to Ronni
NO!
NO!
Have a l i fe!
N = N+1
N < 14?
Yes.
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Attend LAB N
Interesting?Useful?
NO! Send email with LAB N and comments to Ronni
Yes.
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Yes.Interesting?Useful?
Go through exercisesSend email with N and
comments to Ronni
NO!
NO!
Have a l i fe!
N = N+1
N < 14?
Yes.
NO! Send email with comments to Ronni
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Attend LAB N
Interesting?Useful?
NO! Send email with LAB N and comments to Ronni
Yes.
Listing 1: Class flow
4 / 21
-
The Program . . .
N = 1
Rethink!NO!
I guess
Need to program?
START
Attend class N
Interesting?Useful?
Accessible?
Yes.Play with examples
Yes.Interesting?Useful?
Go through exercisesSend email with N and
comments to Ronni
NO!
NO!
Have a l i fe!
N = N+1
N < 14?
Yes.
NO! Send email with comments to Ronni
N=1: "Introduction"
N=2: "Thinking Programs"
N=3: "Fundamental Principles I"
N=4: "Matlab I"
N=5: "Fundamental Principles II"
...
N=14: "GMT II"
Attend LAB N
Interesting?Useful?
NO! Send email with LAB N and comments to Ronni
Yes.
Exercise #1: What are possib le problems wi th
this program?
Think about t iming, count ing, and general f low.
Listing 1: Class flow
4 / 21
-
The very basics (1)
From ‘The Conscience of a Hacker’, The Mentor (1986):[. . . ] I made a discovery today. I found a computer. Wait a second,this is cool. It does what I want it to. If it makes a mistake, it’sbecause I screwed it up. Not because it doesn’t like me . . .Or feels threatened by me . . .Or thinks I’m a smart ass . . .Or doesn’t like teaching and shouldn’t be here [. . . ]
5 / 21
-
The very basics (2)
Programming is beyond language.
Programming is about writing code that people can read.Code is poetry.RTFM and/or the internet
http://thinkgeek.com
“When I’m writing poetry,it feels like the center ofmy thinking is in aparticular place, andwhen I’m writing code thecenter of my thinking feelsin the same kind of place.”
Richard Gabriel,
Distinguished Engineer at Sun Microsystems
6 / 21
-
The very basics (2)
Programming is beyond language.Programming is about writing code that people can read.
Code is poetry.RTFM and/or the internet
http://thinkgeek.com
“When I’m writing poetry,it feels like the center ofmy thinking is in aparticular place, andwhen I’m writing code thecenter of my thinking feelsin the same kind of place.”
Richard Gabriel,
Distinguished Engineer at Sun Microsystems
6 / 21
-
The very basics (2)
Programming is beyond language.Programming is about writing code that people can read.Code is poetry.
RTFM and/or the internet
http://thinkgeek.com
“When I’m writing poetry,it feels like the center ofmy thinking is in aparticular place, andwhen I’m writing code thecenter of my thinking feelsin the same kind of place.”
Richard Gabriel,
Distinguished Engineer at Sun Microsystems
6 / 21
-
The very basics (2)
Programming is beyond language.Programming is about writing code that people can read.Code is poetry.RTFM and/or the internet
http://thinkgeek.com
“When I’m writing poetry,it feels like the center ofmy thinking is in aparticular place, andwhen I’m writing code thecenter of my thinking feelsin the same kind of place.”
Richard Gabriel,
Distinguished Engineer at Sun Microsystems
6 / 21
-
More Philosophy . . .
Jon Claerbout (a geophysicist), as quoted in “WaveLab andReproducible Research”:
An article about computational science in a scientificpublication is not the scholarship itself, it is merelyadvertising of the scholarship. The actual scholarship is thecomplete software development environment and thecomplete set of instructions which generated the figures.
Implications . . .publications should include data and code (example: Okada)figures should be reproducible by readerswrite code that others can use!
7 / 21
-
More Philosophy . . .
Jon Claerbout (a geophysicist), as quoted in “WaveLab andReproducible Research”:
An article about computational science in a scientificpublication is not the scholarship itself, it is merelyadvertising of the scholarship. The actual scholarship is thecomplete software development environment and thecomplete set of instructions which generated the figures.
Implications . . .publications should include data and code (example: Okada)figures should be reproducible by readerswrite code that others can use!
7 / 21
-
More Philosophy . . .
Jon Claerbout (a geophysicist), as quoted in “WaveLab andReproducible Research”:
An article about computational science in a scientificpublication is not the scholarship itself, it is merelyadvertising of the scholarship. The actual scholarship is thecomplete software development environment and thecomplete set of instructions which generated the figures.
Implications . . .publications should include data and code (example: Okada)figures should be reproducible by readerswrite code that others can use!
7 / 21
-
What does that mean?
Good1 function fp = screw2d ( x , xf , d , sdot )
% f u n c t i o n fp = screw2d ( x , xf , d , sdot )3 %
% Computes f a u l t−p a r a l l e l s l i p ra te f o r 2D screw d i s l o c a t i o n5 % wi th f a u l t loca ted at xf , w i th l ock ing depth d and s l i p ra te sdot .
% W i l l compute a t one or many l o c a t i o n s x .7 %
% x column vec to r9 % x f sca la r
% d sca la r11 % sdot sca la r
%13 i f ( d == 0 )
fp = sdot∗0.5∗sign ( x−x f∗ones ( size ( x ) ) ) ;15 else
fp = sdot∗atan2 ( ( x−x f∗ones ( size ( x ) ) ) , d ) / pi ;17 end
Badfunction fp = screw2d ( x , xf , d , sdot )
2 i f ( d==0) fp=sdot∗0.5∗sign ( x−x f∗ones ( size ( x ) ) ) ; else fp=sdot∗atan2 ( ( x−x f∗ones ( size ( x ) ) ) , d ) / pi ;end
8 / 21
-
What does that mean?
Good1 function fp = screw2d ( x , xf , d , sdot )
% f u n c t i o n fp = screw2d ( x , xf , d , sdot )3 %
% Computes f a u l t−p a r a l l e l s l i p ra te f o r 2D screw d i s l o c a t i o n5 % wi th f a u l t loca ted at xf , w i th l ock ing depth d and s l i p ra te sdot .
% W i l l compute a t one or many l o c a t i o n s x .7 %
% x column vec to r9 % x f sca la r
% d sca la r11 % sdot sca la r
%13 i f ( d == 0 )
fp = sdot∗0.5∗sign ( x−x f∗ones ( size ( x ) ) ) ;15 else
fp = sdot∗atan2 ( ( x−x f∗ones ( size ( x ) ) ) , d ) / pi ;17 end
Badfunction fp = screw2d ( x , xf , d , sdot )
2 i f ( d==0) fp=sdot∗0.5∗sign ( x−x f∗ones ( size ( x ) ) ) ; else fp=sdot∗atan2 ( ( x−x f∗ones ( size ( x ) ) ) , d ) / pi ;end
8 / 21
-
Outline
1 Overview and Philosophies
2 Thinking programs
3 Building programs
4 Summary
9 / 21
-
Thinking programs – Breaking down complex tasks
Example 1:
Getting into grad school ... andout.
things to do:apply, figure out where to go, visa stuff, class work, research, thesis . . .
10 / 21
-
Thinking programs – Breaking down complex tasks
Example 1:
Getting into grad school ... andout.
things to do:apply, figure out where to go, visa stuff, class work, research, thesis . . .
10 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
search web
talk to advisors
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
search web
talk to advisors
GRE, TOEFL
paperwork
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
search web
talk to advisors
GRE, TOEFL
paperwork
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
make decision
search web
talk to advisors
GRE, TOEFL
paperwork
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
make decision
move
search web
talk to advisors
GRE, TOEFL
paperwork
do visa stuff
pack and leave
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
make decision
move
do classwork do research write papers
search web
talk to advisors
GRE, TOEFL
paperwork
do visa stuff
pack and leave
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
make decision
move
do classwork do research write papers
search web
talk to advisors
GRE, TOEFL
paperwork
do visa stuff
pack and leave
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
make decision
move
do classwork do research write papers
write thesis
defend
search web
talk to advisors
GRE, TOEFL
paperwork
do visa stuff
pack and leave
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
make decision
move
do classwork do research write papers
write thesis
defend
search web
talk to advisors
GRE, TOEFL
paperwork
do visa stuff
pack and leave
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Find schools
apply
wait ...
make decision
move
do classwork do research write papers
write thesis
defend
search web
talk to advisors
GRE, TOEFL
paperwork
do visa stuff
pack and leave
find a job
Listing 1: Seminar flow
11 / 21
-
Thinking programs – Breaking down complex tasks
Example 2:
Grad student’s Average Day
possible activities:eat, sleep, work, do stuff, . . .
12 / 21
-
Thinking programs – Breaking down complex tasks
Example 2:
Grad student’s Average Daypossible activities:
eat, sleep, work, do stuff, . . .
12 / 21
-
Thinking programs – Breaking down complex tasks
get up
do morning stuff
go to school
work
lunch
work
dinner
work
go home
sleep
Listing 1: make_my_day
possible implementation1 % make_my_day .m
%−−−−−−−−−−−−−−3 % program t h a t shows how much fun
% l i v e as a grad student i s : )5
clc ;7
getUp ;9 eat ( ’ b reak fas t ’ ) ;
walk ( ’ school ’ ) ;11 work ;
eat ( ’ lunch ’ ) ;13 work ( ) ;
eat ( ’ d inner ’ ) ;15 work ( ) ;
walk ( ’home ’ ) ;17 haveLi fe ;
s leep ;
13 / 21
-
Thinking programs – Breaking down complex tasks
get up
do morning stuff
go to school
work
lunch
work
dinner
work
go home
sleep
Listing 1: make_my_day
possible implementation% make_my_day .m
2 %−−−−−−−−−−−−−−% program t h a t shows how much fun
4 % l i v e as a grad student i s : )
6 clc ;
8 getUp ;eat ( ’ b reak fas t ’ ) ;
10 walk ( ’ school ’ ) ;work ;
12 eat ( ’ lunch ’ ) ;work ( ) ;
14 eat ( ’ d inner ’ ) ;work ( ) ;
16 walk ( ’home ’ ) ;haveLi fe ;
18 sleep ;
13 / 21
-
Outline
1 Overview and Philosophies
2 Thinking programs
3 Building programs
4 Summary
14 / 21
-
Building programs – One black box at a time
Strategies to implement a program:
Top downSame as the examples above:
start with the big pictureidentify reasonablesubtaskstry to divide things to a levelof managable complexity(atoms)implement atomsimplement main routine(flow control)
Bottom upproblems accumulateimplement an atom at thetimeat some point you figure outthat things could gotogetherrevise main routineconstantlyadd necessary subroutines
15 / 21
-
Building programs – One black box at a time
Strategies to implement a program:
Top downSame as the examples above:
start with the big pictureidentify reasonablesubtaskstry to divide things to a levelof managable complexity(atoms)implement atomsimplement main routine(flow control)
Bottom upproblems accumulateimplement an atom at thetimeat some point you figure outthat things could gotogetherrevise main routineconstantlyadd necessary subroutines
15 / 21
-
Building programs – One black box at a time
Bottom lineTry building tools that solve a set of similar problems in a genericway. Use Parameters!Build and test each atom individually, test all scenarios (and more)with synthetic input.Treat atoms as black boxes that implement desired functionality.Don’t care about them once they’re working
16 / 21
-
Building programs – One black box at a time
Keys to good programsModularity: split problem in manageable tasks, implement andtest one at a time
Reusability: write functions, avoid redundance, avoid monolithiccode (theoretically one loop would be enough)
Pidd, 2002
Generalize: use variables instead of hard coded values, handparameters to functionsFunctionality, then efficiency
17 / 21
-
Building programs – One black box at a time
Keys to good programsModularity: split problem in manageable tasks, implement andtest one at a timeReusability: write functions, avoid redundance, avoid monolithiccode (theoretically one loop would be enough)
Full (composable) simulation model reuse
Function reuse
Copy ’n’ paste
Frequency:
Complexity:
low
lowhigh
high
Software com- ponent reuse
Pidd, 2002
Generalize: use variables instead of hard coded values, handparameters to functionsFunctionality, then efficiency
17 / 21
-
Building programs – One black box at a time
Keys to good programsModularity: split problem in manageable tasks, implement andtest one at a timeReusability: write functions, avoid redundance, avoid monolithiccode (theoretically one loop would be enough)
Full (composable) simulation model reuse
Function reuse
Copy ’n’ paste
Frequency:
Complexity:
low
lowhigh
high
Software com- ponent reuse
Pidd, 2002
Generalize: use variables instead of hard coded values, handparameters to functions
Functionality, then efficiency
17 / 21
-
Building programs – One black box at a time
Keys to good programsModularity: split problem in manageable tasks, implement andtest one at a timeReusability: write functions, avoid redundance, avoid monolithiccode (theoretically one loop would be enough)
Full (composable) simulation model reuse
Function reuse
Copy ’n’ paste
Frequency:
Complexity:
low
lowhigh
high
Software com- ponent reuse
Pidd, 2002
Generalize: use variables instead of hard coded values, handparameters to functionsFunctionality, then efficiency
17 / 21
-
Building programs
The Control Routine% make_my_day .m
2 %−−−−−−−−−−−−−−% program t h a t shows how much fun
4 % l i v e as a grad student i s : )
6 clc ;
8 getUp ;eat ( ’ b reak fas t ’ ) ;
10 walk ( ’ school ’ ) ;work ;
12 eat ( ’ lunch ’ ) ;work ( ) ;
14 eat ( ’ d inner ’ ) ;work ( ) ;
16 walk ( ’home ’ ) ;haveLi fe ;
18 sleep ;
Using Parameters% eat .m
2 %−−−−−−−−−−−−−−function eat ( what )
4 f p r i n t f (1 , ’%s : yummy . . . %s \ n ’ , . . .mfilename , what ) ;
6 pause ( 1 ) ;end
18 / 21
-
Outline
1 Overview and Philosophies
2 Thinking programs
3 Building programs
4 Summary
19 / 21
-
Summary – Take home messages
Thinking . . .Think modularThink in general casesThink non-redundantThink about reuseThink about reproducibility
Exercising . . .Read other peoples’ code . . . criticallyThe first version is for the trash bin (unintentionally)
Truth . . .Your working environment will change, concepts likely survive! Beflexible in the choice of languages and tools.
20 / 21
-
Summary – Take home messages
Thinking . . .Think modularThink in general casesThink non-redundantThink about reuseThink about reproducibility
Exercising . . .Read other peoples’ code . . . criticallyThe first version is for the trash bin (unintentionally)
Truth . . .Your working environment will change, concepts likely survive! Beflexible in the choice of languages and tools.
20 / 21
-
If all fails . . .
“The Ballmer Peak”
http://www.xkcd.com/323/
21 / 21
Overview and PhilosophiesThinking programsBuilding programsSummary