Code Smell 117 - Unrealistic Data
Tags: oop, javascript, tutorial, webdev
Published: 2022-03-03 03:03:12


*Programmers are lazy and seldom try to learn from real business domains*

> TL;DR: Use real case scenarios and real data (when possible)

# Problems

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

- Bad test use cases

- Readability

# Solutions

1. Change test data for a real one.

2. Use [MAPPER](https://dev.to/mcsee/the-one-and-only-software-design-principle-3086) to map real entities and real data.

# Context

In the past, developers used to fake domain data.

We considered [Hello Word](https://dev.to/mcsee/we-should-get-rid-of-helloworld-forever-43om) a good practice and we tested with abstract data.

We developed using a [waterfall model](https://en.wikipedia.org/wiki/Waterfall_model) very far from real users.

With bijection and MAPPER techniques, DDD and [TDD](https://dev.to/mcsee/tdd-conference-2021-all-talks-1559), [user acceptance testing](https://en.wikipedia.org/wiki/Acceptance_testing) became more important.

Using Agile methodologies, we need to test with real-world data.

If we find an error in a production system, we need to add a case covering the exact mistake with real data.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/d9b312e97d7233738ea06f322ae41da1)
```python
class BookCartTestCase(unittest.TestCase):
    def setUp(self):
        self.cart = Cart()

    def test_add_book(self):
       self.cart.add_item('xxxxx', 3, 10)
        #This is not a real example

       self.assertEqual(self.cart.total, 30, msg='Book Cart total not correct after adding books')
       self.assertEqual(self.cart.items['xxxxx'], 3, msg='Quantity of items not correct after adding book')
 
    def test_remove_item(self):
        self.cart.add_item('fgdfhhfhhh', 3, 10)
        self.cart.remove_item('fgdfhhfhrhh', 2, 10)    
        #We made a typo since example is not a real one
        self.assertEqual(self.cart.total, 10, msg='Book Cart total not correct after removing book')
        self.assertEqual(self.cart.items['fgdfhhfhhh'], 1, msg='Quantity of books not correct after removing book')
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/539d4699db494d180219620b7baeedea)
```python
class BookCartTestCase(unittest.TestCase):
    def setUp(self):
        self.cart = Cart()

    def test_add_book(self):
       self.cart.add_item('Harry Potter', 3, 10)

       self.assertEqual(self.cart.total, 30, msg='Book Cart total not correct after adding books')
       self.assertEqual(self.cart.items['Harry Potter'], 3, msg='Quantity of items not correct after adding book')

    #We don't reuse same example. 
    #We use a new REAL book
    def test_remove_item(self):
        self.cart.add_item('Divergent', 3, 10)
        self.cart.remove_item('Divergent', 2, 10)    
        self.assertEqual(self.cart.total, 10, msg='Book Cart total not correct after removing book')
        self.assertEqual(self.cart.items['Divergent'], 1, msg='Quantity of books not correct after removing book')

```

# Detection

[X] Manual

This is a semantic smell.

# Tags

- Testing

# Conclusion

Code comments are a [code smell](https://dev.to/mcsee/code-smell-05-comment-abusers-2ba5).

Reading tests is the only way to learn how the software behaves.

We need to be extra explicit on our tests.

# Exceptions

On some domains and under regulation we cannot use real data. 

We should fake it with meaningful data.

# Relations

{% post https://dev.to/mcsee/code-smell-05-comment-abusers-2ba5 %}

# More Info

- [Given/Then/When](https://en.wikipedia.org/wiki/Given-When-Then)

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

# Credits

Photo by [Hofmann Natalia](https://unsplash.com/@natali333) on [Unsplash](https://unsplash.com/s/photos/surreal)

Thanks to [Curtis Einsmann](https://twitter.com/curtiseinsmann/status/1487118139196420099)
  
* * *

> You do not really understand something unless you can explain it to your grandmother.

_Albert Einstein_

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