Code Smell 244 - Incomplete Error information
Tags: webdev, beginners, programming, tutorial
Published: 2024-03-31 00:16:31

*You show an error and provide no useful information*

> TL;DR: Help yourself and others with correction information

# Problems

- Debugging and maintenance challenge.

- Fail Fast Principle violation

- Debugging complex situations.

# Solutions

1. Add all the relevant information to solve the solution

# Context

When you are reporting an error, either via an information text in the UI, by processing an API request, or by creating a test assertion, you need to provide an exit (a possible solution).

This is very relevant when dealing with complex scenarios, large objects, or arrays with minimal mistakes.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/9427d3e30d0485bf431a0dd21db3a40d)
```python
VALID_COLUMNS = ['name', 'gender', 'email']

def process_API_information(data):
    invalid_columns = []
    for column in data.keys():
        if column not in VALID_COLUMNS:
            invalid_columns.append(column)
    
    assert not invalid_columns, "Invalid columns detected."  
    # No details were provided about which columns are invalid
     
 
data = {'name': 'John', 'gender': 'Pangender', 
        'age': 47, 'email': 'john@example.com'}
process_API_information(data)
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/b7eb7b4def657b3798dea3c0bdcf0809)
```python
VALID_COLUMNS = ['name', 'gender', 'email']

def process_API_information(data):
    invalid_columns = [
        column for column in data.keys() if column not in VALID_COLUMNS
    ]
    
    if invalid_columns:
        raise ValueError(
            f"Invalid columns detected: {', '.join(invalid_columns)}"
        )  # Shows WHICH columns are invalid

data = {'name': 'John', 'gender': 'Pangender', 
        'age': 47, 'email': 'john@example.com'}
process_API_information(data)
```

# Detection

[X] Semi-Automatic 

This is a semantic smell. You can warn the developers on error texts that do not include variables. 

# Tags

- Errors

# Level

[X] Beginner 

# AI Assistants

AI assistants usually miss this kind of help and provide hardcoded error messages.

# Conclusion

You need to always think about how to help your end users. 

It might be yourself.

# Relations

{% post https://dev.to/mcsee/code-smell-104-assert-true-20db %}

{% post https://dev.to/mcsee/code-smell-97-error-messages-without-empathy-4lpo %}

# 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 [Brett Jordan](https://unsplash.com/@brett_jordanh) on [Unsplash](https://unsplash.com/photos/brown-wooden-blocks-on-white-surface-Xp9WOzF92Jw)  
  
* * *

> Information shared by an object might or might not be part of the structure of that object. That is, the object might compute the information, or it might delegate the request for information to another object.

_Rebecca Wirfs Brooks_ 
 
{% 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 %}