6. Dragon ADR Position Set¶
ADR - Architecture Design Records
6.1. Problem¶
Set new position to x=10, y=20
6.2. Option 1¶
>>> dragon.fly(10, 20)
>>> dragon.teleport(10, 20)
>>> dragon.set_position(10, 20)
>>> dragon.set_position_xy(10, 20)
Good: easy to use
Good: encapsulation
Good: easy to add validation if needed
Good: easy to extend to 3D - add parameter with default value
0
Bad:
teleport()
andfly()
are bad namesBad:
set_position_xy()
ties you to 2D pointBad: arguments are implicit, require knowledge of an API
6.3. Option 2¶
>>> dragon.fly(x=10, y=20)
>>> dragon.teleport(x=10, y=20)
>>> dragon.set_position(x=10, y=20)
Good: easy to use
Good: arguments are explicit
Good: encapsulation
Good: easy to add validation if needed
Good: easy to extend to 3D - add parameter with default value
0
Bad:
teleport()
andfly()
are bad names
6.4. Option 3¶
>>> dragon.set(position_x=10, position_y=20)
Good: easy to use
Good: arguments are explicit
Good: encapsulation
Good: easy to add validation if needed
Bad:
set()
is to genericBad: gateway to abuse
6.5. Option 4¶
>>> dragon.x = 10
>>> dragon.y = 20
>>> dragon.x, dragon.y = 10, 20
Good: easy to use
Good: arguments are explicit
Good: can use
@property
for validation if neededBad: encapsulation
Bad: names
x
andy
are weakly related todragon
Example:
>>> knn.w = [1,2,3]
6.6. Option 5¶
>>> dragon.position_x = 10
>>> dragon.position_y = 20
>>> dragon.position_x, dragon.position_y = 10, 20
Good: easy to use
Good: arguments are explicit
Good: can use
@property
for validation if neededBad: encapsulation
Example:
>>> knn.weights = [1,2,3]
6.7. Option 6¶
>>> dragon.position = (10, 20)
>>> dragon.position @ (10, 20)
Good: easy to use
Good: can use
@property
for validation if neededGood: using
@
(matmul) it is easy to validationBad: arguments are implicit
Bad: require knowledge of an API
Bad: always 2D
Bad: not extensible, hard to refactor to 3D
6.8. Option 7¶
>>> dragon.position = Point(x=10, y=20)
>>> dragon.position @ Point(x=10, y=20)
Good: easy to use
Good: can use
@property
for validation if neededGood: arguments are explicit
Good: readability
Bad: require knowledge of an API
Bad: extensible, easy to refactor to 3D
6.9. Option 8¶
>>> dragon.position.x = 10
>>> dragon.position.y = 20
>>> dragon.position.x, dragon.position.y = 10, 20
Good: more or less easy to use
Good: arguments are explicit
Good: can use
@property
for validation if neededGood: encapsulation
Good: more or less readable
Bad: extensible, easy to refactor to 3D
Bad: nested
Bad: require knowledge of an API
6.10. Decision¶
>>> dragon.set_position(x=10, y=20)
Good: easy to use
Good: arguments are explicit
Good: encapsulation
Good: easy to add validation if needed
Good: extensible, easy to refactor to 3D