36 lines
1.1 KiB
Plaintext
36 lines
1.1 KiB
Plaintext
Just because you _can_ do something, doesn't mean you should, and that's the case with nested conditional expressions. Nesting conditional expressions results in the kind of code that may seem clear as day when you write it, but six months later will leave maintainers (or worse - future you) scratching their heads and cursing.
|
|
|
|
|
|
Instead, err on the side of clarity, and use another line to express the nested operation as a separate statement.
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
class Job:
|
|
@property
|
|
def readable_status(self):
|
|
return "Running" if job.is_running else "Failed" if job.errors else "Succeeded" # Noncompliant
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
class Job:
|
|
@property
|
|
def readable_status(self):
|
|
if job.is_running:
|
|
return "Running"
|
|
return "Failed" if job.errors else "Succeeded"
|
|
----
|
|
|
|
== Exceptions
|
|
|
|
No issue is raised on conditional expressions in comprehensions.
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|