Code Smell 244 - Incomplete Error Information and How to Fix It

Written by mcsee | Published 2024/04/01
Tech Story Tags: software-development | software-engineering | clean-code | code-smells | common-code-smells | incomplete-error-information | fix-your-code

TLDRHelp yourself and others with correction informationvia the TL;DR App

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': '[email protected]'}
process_API_information(data)

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': '[email protected]'}
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

Code Smell 104 - Assert True

Code Smell 97 - Error Messages Without Empathy

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

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code


Written by mcsee | I’m senior software engineer specialized in declarative designs and S.O.L.I.D. and Agile lover.
Published by HackerNoon on 2024/04/01