Code Smell 234 - Long Circuit
Tags: webdev, programming, beginners, python
Published: 2023-12-16 23:02:06

*Be smart (and lazy) with low performant conditions*

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

# Problems

- Low Performance

# Solutions

1. Sort the conditions from faster to slower

# Context

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

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/ddef91c2fc31de6da80b8c5b244ff904)
```python
def is_warm():
    # This is a fast api call to your thermometer
    response = requests.get
        ("https://iot-device-api.example.com/current_temperature")
    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
        ("https://calendar-api.example.com/today")
    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
        ("https://weather-api.example.com/current")
    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!")
else:
    print("Stay at home.")
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/2d9bb0dcab1d80de01358e8259412cf4)
```python
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!")
else:
    print("Stay at home.")
```

# Detection

[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).

# Tags

- Performance

# Conclusion

Find bottlenecks using Pareto rules. 

Optimize your code-critical sections.

# Relations

{% post https://dev.to/mcsee/code-smell-140-short-circuit-evaluation-20kc %}

{% post https://dev.to/mcsee/code-smell-145-short-circuit-hack-4l4p %}

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Nick Abrams](https://unsplash.com/@nbabrams) on [Unsplash](https://unsplash.com/photos/brown-tortoise-on-brown-sand-FTKfX3xZIcc)
    
* * *

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

_Jon Bentley and Douglas McIlroy_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}