Empty and Implicit Returns cause resiliency issues.
Counts one violation each time a function has at least one significant return (expression different from nothing, none or false) and in the same time :
- Â at least one empty return
OR - an implicit return in place of the last statement.
bad
def foo(x):
if x >= 0:
return math.sqrt(x)
# <— implicit return here
def bar(x):
if x < 0:
return # <—- empty return here
return math.sqrt(x)
good
def foo(x):
if x >= 0:
return math.sqrt(x)
else:
return Nonedef bar(x):
if x < 0:
return None
return math.sqrt(x)
Why you should care
Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None , and an explicit return statement should be present at the end of the function (if reachable).
Business Impacts
CAST recommendations
References
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.