Code Smell 234 - Long Circuit

Photo by Nick Abrams on Unsplash

Code Smell 234 - Long Circuit

Be smart (and lazy) with low performance conditions

TL;DR: Premature Optimization is Evil. Optimization is Good.


  • Low Performance


  1. Sort the conditions from faster to slower


Readability is always essential and you should avoid premature optimization.

Non-premature optimization happens when you have actual evidence you can improve your code execution time without much readability penalizations.

Sample Code


def is_warm():
    # This is a fast api call to your thermometer
    response = requests.get("")
    temperature_data = response.json()

    return temperature_data.get('temperature', 0) > 25  

def is_weekend():
    # This function checks if today is a weekend based on a slow calendar API call
    response = requests.get("")
    calendar_data = response.json()

    return calendar_data.get('day_of_week', '').lower() in ['saturday', 'sunday']

def is_sunny():
    # Very slow function to a low performant weather API call
    response = requests.get("")
    weather_data = response.json()

    return weather_data.get('weather', '') == 'sunny'

is_sunny_value = is_sunny()
is_warm_value = is_warm()
is_weekend_value = is_weekend()  

if is_sunny_value and is_warm_value and is_weekend_value:
    # the 3 conditions are always evaluated
    print("Let's go outside!")
    print("Stay at home.")


if is_warm() and is_weekend() and is_sunny():
    # the 3 conditions are evaluated in short circuit 
    # and sorted from fastest to slowest
    # for a fast exit
    print("Let's go outside!")
    print("Stay at home.")


[X] Semi-Automatic

You can detect slow calls using actual benchmarks.

Do not consider algorithm complexity since sometimes it is unrelated to actual data distribution. (for example, optimizing an array with a few elements).


  • Performance


Find bottlenecks using Pareto rules.

Optimize your code-critical sections.



Code Smells are my opinion.


Photo by Nick Abrams on Unsplash

The key to performance is elegance, not battalions of special cases.

Jon Bentley and Douglas McIlroy

This article is part of the CodeSmell Series.