Slthy / Microplus-Lenex

Web scraper and Lenex encoder for swimming events hosted by Microplus s.r.l
MIT License
1 stars 0 forks source link

Condition inversion/early return for clarity #18

Closed CremaLuca closed 1 year ago

CremaLuca commented 1 year ago

In utils.format_time hai la seguente condizione

https://github.com/Slthy/Microplus-Lenex/blob/9e0c07df9bea626b55bf0dc7c8c8e563c21bbd53/utils.py#L90

Il codice continua di una indentazione per il percorso "felice" se va tutto bene, altrimenti restituisce "NT"

https://github.com/Slthy/Microplus-Lenex/blob/9e0c07df9bea626b55bf0dc7c8c8e563c21bbd53/utils.py#L96

Secondo me (e secondo altri con più esperienza nella programmazione) è meglio applicare quella che si chiama condition inversion, che consiste nel concentrare i casi "brutti" all'inizio del codice ed evitare una indentazione.

# Time too short, invalid (e.g. "dnf")
if len(time) < 4:
    return "NT"

if len(time) > 5:
    ...
Slthy commented 1 year ago

vero, la funzione diventa più comoda da leggere

CremaLuca commented 1 year ago

No fra, non serve l'else dopo un return

Slthy commented 1 year ago

cazzo vero, stupid af