3.5. ADR Dragon Create Noname
EN: Dragon on creation display error if it does not have name
PL: Smok przy tworzeniu wyświetla błąd jeżeli nie ma imienia
3.5.1. Option 1
>>> print('Dragon must have a name')
Dragon must have a name
Good:
Easy to use
User friendly
Bad:
You should never print in a class
Hard to catch programmatically
Less verbose than exception
Example:
>>> dragon = Dragon()
Dragon must have a name
Decision:
Rejected, hard to catch programmatically
3.5.2. Option 2
>>> logging.error('Dragon must have a name')
ERROR:root:Dragon must have a name
Good:
Easy to use
User friendly (more or less)
Way better than print
Bad:
Hard to catch programmatically
Less verbose than exception
Example:
>>> dragon = Dragon()
ERROR:root:Dragon must have a name
Decision:
Rejected, hard to catch programmatically
3.5.3. Option 3
>>> raise NameError
Traceback (most recent call last):
NameError:
Good:
Easy to use
Easy to understand
Much better than print and logging
Easy to catch programmatically
Bad:
Without message it is hard to understand what happened
Less user friendly
Example:
>>> dragon = Dragon()
Traceback (most recent call last):
NameError:
Decision:
Rejected, without message it is hard to understand what happened
3.5.4. Option 4
>>> raise NameError('Dragon must have a name')
Traceback (most recent call last):
NameError: Dragon must have a name
Good:
Easy to use
Easy to understand
Easy to catch programmatically
Verbose
Bad:
Requires default value for name, and later checking it
Example:
>>> dragon = Dragon()
Traceback (most recent call last):
NameError: Dragon must have a name
Implementation:
>>> class Dragon:
... name: str
...
... def __init__(self, name: str = None) -> None:
... if name is None:
... raise NameError('Dragon must have a name')
... self.name = name
Decision:
Candidate
3.5.5. Option 5
>>> dragon = Dragon()
Traceback (most recent call last):
TypeError: Dragon.__init__() missing 1 required positional argument: 'name'
Good:
Easy to use
Easy to understand
Much better than print and logging
Easy to catch programmatically
Verbose
Works out of the box (no code required)
Less code to maintain
Standard Python message that all developers know
Bad:
Very technical error message (not user friendly)
Example:
>>> dragon = Dragon()
Traceback (most recent call last):
TypeError: ...
Implementation:
>>> class Dragon:
... name: str
...
... def __init__(self, name: str, /) -> None: ...
Decision:
Candidate
3.5.6. Decision
>>> dragon = Dragon()
Traceback (most recent call last):
TypeError: Dragon.__init__() missing 1 required positional argument: 'name'
Rationale:
Easy to use and understand
Easy to catch programmatically
Works out of the box (no code required)
Standard Python message that all developers know
Less code to maintain
Implementation:
>>> class Dragon:
... def __init__(self, name: str, /) -> None: ...