Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ Code Smell 234 - Long Circuit

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Code Smell 234 - Long Circuit


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

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

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

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

Disclaimer

Code Smells are my opinion.

Credits

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.

...



๐Ÿ“Œ Code Smell 184 - Exception Arrow Code


๐Ÿ“ˆ 28.52 Punkte

๐Ÿ“Œ Code Smell 230 - Schrรถdinger Code


๐Ÿ“ˆ 28.52 Punkte

๐Ÿ“Œ Code Smell 232 - Reusable Code


๐Ÿ“ˆ 28.52 Punkte

๐Ÿ“Œ Code Smell 205 - Code in Destructors


๐Ÿ“ˆ 28.52 Punkte

๐Ÿ“Œ PyPi, Wordpress, Hikvision, Zimbra, Palo Alto, & LED Morse Code - SWN #234


๐Ÿ“ˆ 25.67 Punkte

๐Ÿ“Œ Agilebio Lab Collector 4.234 Remote Code Execution


๐Ÿ“ˆ 25.67 Punkte

๐Ÿ“Œ [webapps] Agilebio Lab Collector Electronic Lab Notebook v4.234 - Remote Code Execution (RCE)


๐Ÿ“ˆ 25.67 Punkte

๐Ÿ“Œ Java 15: Sealed Classes - Code-Smell oder moderne Erweiterung?


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 189 - Not Sanitized Input


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 246 - Expiration Date


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 247 - Javascript Replace


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 248 - Unreliable Copy


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 249 - Constants as Numbers


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 250 - Premature Memoization


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 195 - Yoda Conditions


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 197 - Gratuitous Context


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 203 - Irrelevant Test Information


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 229 - Red Tape


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 235 - Console Side Effects


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 236 - Unwrapped Lines


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 241- Referential Transparency Violation


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 242 - Zombie Feature Flags


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 244 - Incomplete Error information


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 200 - Poltergeist


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 209 - Side Effects


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Code Smell 245 - exec() and eval()


๐Ÿ“ˆ 24.65 Punkte

๐Ÿ“Œ Tracking: Forscher finden Ultraschall-Spyware in 234 Android-Apps


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ Tracking: Forscher finden Ultraschall-Spyware in 234 Android-Apps


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ Linux: Systemd 234 erweitert Netzwerk-Komponente


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ Humble Classic Return Bundle: Spiele im Wert von 234 Dollar


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ Building Blocks of AI Interpretability | Two Minute Papers #234


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ Latest Hacking News Podcast #234


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ September 17, 2019 โ€“ HNN #234


๐Ÿ“ˆ 21.8 Punkte

๐Ÿ“Œ Cisco UCS C-Series Rack Servers 3.0(0.234) TCP Throttling TCP SYN Packet Denial of Service


๐Ÿ“ˆ 21.8 Punkte











matomo