Code Smell 198 - Hidden Assumptions
Tags: webdev, beginners, programming, tutorial
Published: 2023-03-02 01:52:46

*Software is about contracts and ambiguous contracts are a nightmare*

> TL;DR: Keep your code explicit

# Problems

- [Coupling](https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7)

- [Fail Fast](https://dev.to/mcsee/fail-fast-48dm) Principle violation

- [Bijection](https://dev.to/mcsee/the-one-and-only-software-design-principle-3086) Fault

# 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

[Gist Url]: # (https://gist.github.com/mcsee/33f7a662d4394e6c94c6603f88d9e975)
```python
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 
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/a0ee2b3ec2e963149bb2b39a9cfa1a08)
```python
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

```

# 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

{% post https://dev.to/mcsee/code-smell-02-constants-and-magic-numbers-obb %}

# More Info

[Mars Climate Orbiter Disaster](https://solarsystem.nasa.gov/missions/mars-climate-orbiter/in-depth/)

{% post https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7 %}

[Measure Solution](https://www.semanticscholar.org/paper/Arithmetic-with-measurements-on-dynamically-typed-Wilkinson-Prieto/40ac4b9918f8fa71fde88449ce9261857317c192)

# 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 [Christian Pfeifer](https://unsplash.com/@sailingaroundtheworld) on [Unsplash](https://unsplash.com/photos/l6OraG-v0d8)

* * *

> 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_

{% 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 %}