Code Smell 117 — Unrealistic Data

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

Maximiliano Contieri
2 min readMar 3, 2022
Photo by Hofmann Natalia on Unsplash

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

Problems

  • Bijection Violation
  • Bad test use cases
  • Readability

Solutions

  1. Change test data for a real one.
  2. Use MAPPER to map real entities and real data.

Context

In the past, developers used to fake domain data.

We considered Hello Word a good practice and we tested with abstract data.

We developed using a waterfall model very far from real users.

With bijection and MAPPER techniques, DDD and TDD, user 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

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

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.

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

More Info

Credits

Thanks to Curtis Einsmann

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

Albert Einstein

--

--

Maximiliano Contieri
Maximiliano Contieri

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet