DEV Community

Cover image for Code Smell 238 - Entangled Code
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

2

Code Smell 238 - Entangled Code

You execute code. Move to the other stuff, and continue with the previous code

TL;DR: Don't mix your train of thought

Problems

  • Readability

  • Bad Scoping

Solutions

  1. Move the code close together

  2. Try to extract the method

Refactorings

Context

Entangled code is related beyond time and space.

You are reading the code, then skip to another subject and return to the first one.

Sample Code

Wrong

def planetary_properties(semi_major_axis,
                         incoming_radiation, reflected_radiation):
    Gravitational_Constant = 1.0    
    Sun_Mass = 1.0    
    # Up to here, there's a preparation
    # for the orbital period computation

    albedo = reflected_radiation / incoming_radiation
    # This is unrelated to the previous computation

    # You resume the first computation
    orbital_period_squared = (
        (4 * math.pi**2 * semi_major_axis**3) /
        (Gravitational_Constant * Sun_Mass)
    )
    retrun orbital_period, albedo
Enter fullscreen mode Exit fullscreen mode

Right

def planetary_properties(semi_major_axis,
                         incoming_radiation, reflected_radiation):
    Gravitational_Constant = 1.0    
    Sun_Mass = 1.0    
     orbital_period_squared = (
        (4 * math.pi**2 * semi_major_axis**3) /
        (Gravitational_Constant * Sun_Mass)
    )
    # This is related to the first computation part

    albedo = reflected_radiation / incoming_radiation
    # This is related to the second part

    # The final solution is to break the function into two
    # This is a trivial example for illustration purposes
    # Things usually get more complicated and entangled

    retrun orbital_period, albedo
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

Some linters can infer scopes and make suggestions.

Tags

  • Readability

Level

[X] Beginner

AI Assistants

AI assistants suggest code without this mistake and improve this problem when asked.

Conclusion

This is a tiny tip and a short example of tidying

Relations

Disclaimer

Code Smells are my opinion.

Credits

Photo by Michael Hamments on Unsplash


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

Brian Kernighan


This article is part of the CodeSmell Series.

DevCycle image

OpenFeature Multi-Provider: Enabling New Feature Flagging Use-Cases

DevCycle is the first feature management platform with OpenFeature built in. We pair the reliability, scalability, and security of a managed service with freedom from vendor lock-in, helping developers ship faster with true OpenFeature-native feature flagging.

Watch Full Video 🎥

Top comments (0)

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay