DEV Community

Cover image for Code Smell 244 - Incomplete Error information
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

4 1 1 1 1

Code Smell 244 - Incomplete Error information

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

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)
Enter fullscreen mode Exit fullscreen mode

Right

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)
Enter fullscreen mode Exit fullscreen mode

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

Disclaimer

Code Smells are my opinion.

Credits

Photo by Brett Jordan on Unsplash


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


This article is part of the CodeSmell Series.

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

Top comments (0)

Build gen AI apps that run anywhere with MongoDB Atlas

Build gen AI apps that run anywhere with MongoDB Atlas

MongoDB Atlas bundles vector search and a flexible document model so developers can build, scale, and run gen AI apps without juggling multiple databases. From LLM to semantic search, Atlas streamlines AI architecture. Start free today.

Start Free

👋 Kindness is contagious

Discover this thought-provoking article in the thriving DEV Community. Developers of every background are encouraged to jump in, share expertise, and uplift our collective knowledge.

A simple "thank you" can make someone's day—drop your kudos in the comments!

On DEV, spreading insights lights the path forward and bonds us. If you appreciated this write-up, a brief note of appreciation to the author speaks volumes.

Get Started