DEV Community

Cover image for Code Smell 198 - Hidden Assumptions
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3

Code Smell 198 - Hidden Assumptions

Software is about contracts and ambiguous contracts are a nightmare

TL;DR: Keep your code explicit

Problems

Solutions

  1. Be declarative and explicit

  2. Don't oversimplify

Context

Hidden assumptions are underlying beliefs or expectations not explicitly stated in the code.

They are still present and can impact the behavior of the software.

Various reasons can give rise to assumptions such as incomplete requirements, incorrect presumptions about the user or environment, limitations of the programming language or tools, and bad accidental decisions.

Sample Code

Wrong

tenCentimeters = 10
tenInches = 10

tenCentimeters + tenInches
# 20
# this error is based on the hidden assumption of a unit (any)
# and caused the Mars Climate Orbiter failure 
Enter fullscreen mode Exit fullscreen mode

Right

class Unit:
    def __init__(self, name, symbol):
        self.name = name
        self.symbol = symbol

class Measure:
    def __init__(self, scalar, unit):
        self.scalar = scalar
        self.unit = unit

    def __str__(self):
        return f"{self.scalar} {self.unit.symbol}"

centimetersUnit = Unit("centimeters", "cm")
inchesUnit = Unit("inches", "in")

tencCentimeters = Measure(10, centimeters)
tenInches = Measure(10, inches)

tenCentimeters + tenInches
# error until we introduce a conversion factor
# in this case the conversion is constant 
# inches = centimeters / 2.54

Enter fullscreen mode Exit fullscreen mode

Detection

[X] Manual

This is a design smell

Tags

  • Coupling

Conclusion

Hidden assumptions can be difficult to identify and can lead to bugs, security vulnerabilities, and usability issues.

To mitigate these risks, software developers should be aware of their assumptions and biases.

Developers also need to engage with users to understand their needs and expectations.

They must test their software in various scenarios to uncover hidden assumptions and edge cases.

Relations

More Info

Mars Climate Orbiter Disaster

Measure Solution

Disclaimer

Code Smells are my opinion.

Credits

Photo by Christian Pfeifer on Unsplash


A human organization is just as much an information system as any computer system. It is almost certainly more complex, but the same fundamental ideas apply. Things that are fundamentally difficult, like concurrency and coupling, are difficult in the real world of people, too.

Dave Farley


This article is part of the CodeSmell Series.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay