dimanche 11 août 2019

Is writing an if statement on one line acceptable?

I'm not sure if this question is appropriate for this community since it's more opinion-oriented, so please vote to close it if it's not.

My question is regarding the if statement in Python (perhaps not just Python either) and the style. Is it acceptable to write it in one line? I'm not talking about the ternary operator of the form something if condition else something-else but rather when people write:

if condition: do this

rather than

if condition:
    do this

I'm confused because I was always taught (and personally believe it's better) to write conditionals in the second form. However, I've noticed that many people write it in the first form.

In what cases would this be acceptable? I couldn't find a particular guideline on this topic online.

Aucun commentaire:

Enregistrer un commentaire