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.
Use lists of lists to implement a 2D grid 3 5 7 7 5 8 6 3 2 4 2 6 5 4
Use lists of lists to implement a 2D grid 3 5 7 7 5 8 6 3 2 4 2 6 5 4
Use lists of lists to implement a 2D grid 3 5 7 7 5 8 6 3 2 4 2 6 5 4
assert N > 0, "Grid size must be positive" # 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) 5
assert N > 0, "Grid size must be positive" # 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) 6
assert N > 0, "Grid size must be positive" # 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) 7
"Aren't meaningful variable names supposed to be a good thing?" # 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) "Aren't meaningful variable names supposed to be a good thing?" 8
grid = [] grid 9
grid = [] EMPTY = [] grid EMPTY 10
grid = [] EMPTY = [] for x in range(N): # x == 0 grid.append(EMPTY) 11
grid = [] EMPTY = [] for x in range(N): # x == 0 grid.append(EMPTY) for y in range(N): # y == 0 grid[-1].append(1) grid EMPTY 1 12
grid = [] EMPTY = [] for x in range(N): # x == 0 grid.append(EMPTY) for y in range(N): # y == 1 grid[-1].append(1) grid EMPTY 1 1 13
grid = [] EMPTY = [] for x in range(N): # x == 0 grid.append(EMPTY) for y in range(N): # y == 2 grid[-1].append(1) grid EMPTY 1 1 1 14
grid = [] EMPTY = [] for x in range(N): # x == 1 grid.append(EMPTY) 15
grid = [] EMPTY = [] for x in range(N): # x == 1 grid.append(EMPTY) for y in range(N): # y == 0 grid[-1].append(1) grid EMPTY 1 1 1 1 16
You see the problem... grid = [] EMPTY = [] for x in range(N): # x == 1 grid.append(EMPTY) for y in range(N): # y == 0 grid[-1].append(1) grid EMPTY 1 1 1 1 You see the problem... 17
Indirection allows aliasing
Indirection allows aliasing Aliasing can be useful
Indirection allows aliasing Aliasing can be useful (In fact, sometimes it's indispensible)
Indirection allows aliasing Aliasing can be useful (In fact, sometimes it's indispensible) But it's also a rich source of bugs
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!
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...
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)
Greg Wilson created by 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.