Source: this Indently video
- Meaningful name
- Type annotations
- Documentation
- Validation of parameters passed in
- Prioritise readability
def vowels(text):
return sum(char.lower() in 'aeiou' for char in text)
# 1 meaningful name
def count_vowels(text):
...
# 2 add type annotations
def count_vowels(text: str) -> int:
...
# 3 documentation
def count_vowels(text: str) -> int:
"""
Counts vowels in text
:param text: ...
:return: ...
"""
# 4 validation
def count_vowels(text: str) -> int:
"""
...
"""
if not isistnace(text,str):
raise TypeError(f"Please only use strings, {type(text)} is not a valid type")
...
# 5 Prioritise readability
def count_vowels(text: str) -> int:
...
num_vowels = 0
for char in text:
if char.lower() in "aeiou":
num_vowels += 1
return num_vowels