3.6. ADR Dragon Health Init

  • EN: Dragon when created has random health from 50 to 100

  • PL: Smok przy tworzeniu ma losowe zdrowie od 50 do 100

3.6.1. Option 1

>>> dragon = Dragon('Name')
>>> dragon.health = randint(50,100)

Usage:

>>> dragon.health
74

Pros and Cons:

  • Good: code is readable

  • Good: easy to use

  • Good: can use property() for validation if needed

  • Bad: requires knowledge of an API

  • Bad: violates encapsulation (OOP Principle)

  • Decision: rejected, violates encapsulation

3.6.2. Option 2

>>> dragon = Dragon('Name')

Usage:

>>> dragon.health
74

Pros and Cons:

  • Good: easy to use

  • Bad: less verbose than keyword arguments

  • Decision: candidate

Example:

>>> dragon = Dragon('Name')
>>> assert dragon.health in range(50,101)
>>> dragon = Dragon('Name')
>>> assert 50 <= dragon.health <= 100
>>> from random import seed; seed(0)
>>>
>>> dragon = Dragon('Name')
>>> dragon.health
74

3.6.3. Option 3

>>> dragon = Dragon('Name')

Usage:

>>> dragon._health
74

Pros and Cons:

  • Good: easy to use

  • Bad: less verbose than public attribute

  • Decision: candidate

Example:

>>> dragon = Dragon('Name')
>>> assert dragon._health in range(50,101)
>>> dragon = Dragon('Name')
>>> assert 50 <= dragon._health <= 100
>>> from random import seed; seed(0)
>>>
>>> dragon = Dragon('Name')
>>> dragon._health
74

3.6.4. Option 4

>>> dragon = Dragon('Name', randint(50,100))

Pros and Cons:

  • Good: easy to use

  • Good: user can set health or newly created dragon

  • Bad: health_min and health_max are outside of the class

  • Bad: not explicit enough

  • Bad: we were asked to generate health randomly

  • Bad: violates Tell, Don't Ask (OOP Principle)

  • Decision: rejected, violates Tell, Don't Ask

3.6.5. Option 5

>>> dragon = Dragon('Name', health=randint(50,100))

Pros and Cons:

  • Good: easy to use

  • Good: explicit enough

  • Good: user can set health or newly created dragon

  • Bad: health_min and health_max are outside of the class

  • Bad: we were asked to generate health randomly

  • Bad: violates Tell, Don't Ask (OOP Principle)

  • Decision: rejected, violates Tell, Don't Ask

3.6.6. Option 6

>>> dragon = Dragon('Name', 50, 100)

Pros and Cons:

  • Good: easy to use

  • Bad: less verbose than keyword arguments

  • Bad: health_min and health_max are outside of the class

  • Bad: requires knowledge of API to answer what is this number

  • Decision: rejected, requires knowledge of API

3.6.7. Option 7

>>> dragon = Dragon('Name', health_min=50, health_max=100)

Pros and Cons:

  • Good: easy to use

  • Good: more verbose than positional arguments

  • Bad: health_min and health_max are outside of the class

  • Bad: requires knowledge of API

  • Decision: candidate

3.6.8. Option 8

>>> dragon = Dragon('Name')
>>> dragon.health_min = 50
>>> dragon.health_max = 100
>>> dragon.init_health()

Pros and Cons:

  • Good: code is readable

  • Good: can use property() for validation if needed

  • Bad: requires knowledge of API

  • Bad: violates encapsulation (OOP Principle)

  • Decision: rejected, violates encapsulation

3.6.9. Option 9

>>> dragon = Dragon('Name')
>>> dragon.HEALTH_MIN = 50
>>> dragon.HEALTH_MAX = 100
>>> dragon.init_health()

Pros and Cons:

  • Good: code is readable

  • Good: can use property() for validation if needed

  • Bad: requires knowledge of API

  • Bad: violates encapsulation (OOP Principle)

  • Decision: rejected, violates encapsulation

3.6.10. Option 10

>>> Dragon.HEALTH_MIN = 50
>>> Dragon.HEALTH_MAX = 100
>>>
>>> dragon = Dragon('Name')

Usage:

>>> dragon.health
74

Pros and Cons:

  • Good: code is readable

  • Good: easy to use

  • Good: can use property() for validation if needed

  • Decision: candidate

Implementation:

>>> class Dragon:
...     HEALTH_MIN = 50
...     HEALTH_MAX = 100
...
...     def __init__(name):
...         self.health = randint(self.HEALTH_MIN, self.HEALTH_MAX)

3.6.11. Decision

>>> Dragon.HEALTH_MIN = 50
>>> Dragon.HEALTH_MAX = 100
>>>
>>> dragon = Dragon('Name')

Rationale:

  • Code is readable

  • Easy to use

  • Easy to modify

  • Can use property() for validation if needed

Implementation:

>>> class Dragon:
...     HEALTH_MIN = 50
...     HEALTH_MAX = 100
...
...     def __init__(name):
...         self.health = randint(self.HEALTH_MIN, self.HEALTH_MAX)

Tests:

>>> from random import seed; seed(0)
>>> Dragon.HEALTH_MIN
50
>>> Dragon.HEALTH_MAX
100
>>> dragon = Dragon('Name')
>>> dragon.health
74