Slide - Software Carpentry

Program Design
Invasion Percolation: Aliasing
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
3
5
7
7
5
8
2
5
6
3
6
3
2
4
5
4
3
Use lists of lists
8
to implement a 2D grid
Program Design
Invasion Percolation
Aliasing
3
5
7
7
5
8
2
5
6
3
6
3
2
4
5
4
3
Use lists of lists
8
to implement a 2D grid
Program Design
Invasion Percolation
Aliasing
3
5
7
7
5
8
2
5
6
3
6
3
2
4
5
4
3
Use lists of lists
8
to implement a 2D grid
Program Design
Invasion Percolation
Aliasing
# Correct code
assert N > 0, "Grid size must be positive"
assert N%2 == 1, "Grid size must be odd"
grid = []
for x in range(N):
grid.append([])
for y in range(N):
grid[-1].append(1)
Program Design
Invasion Percolation
Aliasing
# Incorrect code
assert N > 0, "Grid size must be positive"
assert N%2 == 1, "Grid size must be odd"
grid = []
EMPTY = []
for x in range(N):
grid.append(EMPTY)
for y in range(N):
grid[-1].append(1)
Program Design
Invasion Percolation
Aliasing
# Incorrect code
assert N > 0, "Grid size must be positive"
assert N%2 == 1, "Grid size must be odd"
grid = []
EMPTY = []
for x in range(N):
grid.append(EMPTY)
for y in range(N):
grid[-1].append(1)
Program Design
Invasion Percolation
Aliasing
# Incorrect code
assert N > 0, "Grid size must be positive"
assert N%2 == 1, "Grid size must be odd"
grid = []
EMPTY = []
"Aren't meaningful
for x in range(N):
grid.append(EMPTY)
for y in range(N):
variable names
supposed to be
grid[-1].append(1)
Program Design
a good thing?"
Invasion Percolation
Aliasing
grid = []
gri
d
Program Design
Invasion Percolation
Aliasing
gri
d
Program Design
EMPT
Y
grid = []
EMPTY = []
Invasion Percolation
Aliasing
gri
d
Program Design
EMPT
Y
grid = []
EMPTY = []
for x in range(N): # x == 0
grid.append(EMPTY)
Invasion Percolation
Aliasing
gri
d
grid = []
EMPTY = []
EMPT
Y
1
Program Design
for x in range(N): # x == 0
grid.append(EMPTY)
for y in range(N): # y == 0
grid[-1].append(1)
Invasion Percolation
Aliasing
gri
d
grid = []
EMPTY = []
EMPT
Y
1
1
Program Design
for x in range(N): # x == 0
grid.append(EMPTY)
for y in range(N): # y == 1
grid[-1].append(1)
Invasion Percolation
Aliasing
gri
d
grid = []
EMPTY = []
EMPT
Y
1
1
for x in range(N): # x == 0
grid.append(EMPTY)
for y in range(N): # y == 2
grid[-1].append(1)
1
Program Design
Invasion Percolation
Aliasing
gri
d
grid = []
EMPTY = []
EMPT
Y
for x in range(N): # x == 1
grid.append(EMPTY)
1
1
1
Program Design
Invasion Percolation
Aliasing
gri
d
grid = []
EMPTY = []
EMPT
Y
1
1
for x in range(N): # x == 1
grid.append(EMPTY)
for y in range(N): # y == 0
grid[-1].append(1)
1
1
Program Design
Invasion Percolation
Aliasing
gri
d
grid = []
EMPTY = []
EMPT
Y
for x in range(N): # x == 1
grid.append(EMPTY)
for y in range(N): # y == 0
1
grid[-1].append(1)
1
1
1
You see the problem...
Program Design
Invasion Percolation
Aliasing
Indirection allows aliasing
Program Design
Invasion Percolation
Aliasing
Indirection allows aliasing
Aliasing can be useful
Program Design
Invasion Percolation
Aliasing
Indirection allows aliasing
Aliasing can be useful
(In fact, sometimes it's indispensible)
Program Design
Invasion Percolation
Aliasing
Indirection allows aliasing
Aliasing can be useful
(In fact, sometimes it's indispensible)
But it's also a rich source of bugs
Program Design
Invasion Percolation
Aliasing
Indirection allows aliasing
Aliasing can be useful
(In fact, sometimes it's indispensible)
But it's also a rich source of bugs
When in doubt, draw a picture!
Program Design
Invasion Percolation
Aliasing
Indirection allows aliasing
Aliasing can be useful
(In fact, sometimes it's indispensible)
But it's also a rich source of bugs
When in doubt, draw a picture!
Tools that do this automatically exist...
Program Design
Invasion Percolation
Aliasing
Indirection allows aliasing
Aliasing can be useful
(In fact, sometimes it's indispensible)
But it's also a rich source of bugs
When in doubt, draw a picture!
Tools that do this automatically exist...
...but none has really taken off (yet)
Program Design
Invasion Percolation
Aliasing
created by
Greg Wilson
May 2010
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.