Zum Inhalt

Patterns to adopt

✅ Handle cases with less logic first

Handle cases where you can return something first, all the code that follows does not need to be indented and the code is clearer.

from datetime import datetime

# this is not very readable, return None is implicit here if start or end is not given
def time_between(start: datetime = None, end: datetime = None):
    if start:
        if end:
            return end - start

# this is more readable und we don't need to indent as much
def time_between_better(start: datetime = None, end: datetime = None):
    if not start or not end:
        # same as return None
    return end - start

Letztes Update: June 27, 2022
Erstellt: June 27, 2022