Code Smell 72 - Return Codes
Tags: webdev, programming, codenewbie, tutorial
Published: 2021-05-28 03:05:54

*APIs, Return codes, C Programming Language, We've all been there.*

## TL;DR: Don't return codes to yourself. Raise Exceptions.

# Problems

- [IFs](https://dev.to/mcsee/how-to-get-rid-of-annoying-ifs-forever-1jfg)

- Code Polluting

- Outdated documentation

- Coupling to accidental codes.

- Functional logic polluted.

# Solutions

1. Change Ids and return Generic Exceptions.

2. Distinguish Happy Path from Exception Path.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/d7afaa2f18126a7cdb7ecfcbb1124d81)
```javascript
function createSomething(arguments) {
    //Magic Creation
    success = false; //we failed

    //We failed to create
    if (!success) {
        return {
            object: null,
            errorCode: 403,
            errorDescription: 'We didnt have permission to create...'
        };
    }

    return {
        object: createdObject,
        errorCode: 400,
        errorDescription: ''
    };
}

var myObject = createSomething('argument');
if (myObject.errorCode != 400) {
    console.log(myObject.errorCode + ' ' + myObject.errorDescription)
}
//but myObject does not hold My Object but an implementative
//and accidental array 
//from now on me need to remember this
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/5162daac1e8e7aa5b163ef724944a524)
```javascript
function createSomething(arguments) {
    //Magic Creation
    success = false; //we failed

    //We failed to create
    if (!success) {
        throw new Error('We didnt have permission to create...');
    }

    return createdObject;
}

try {
    var myObject = createSomething('argument');
    //no IFS, just happy path
} catch (exception) {
    //deal with it!
    console.log(exception.message);
}
// myObject holds my expected object
```

# Detection

We can teach our linters to find patterns of integer and strings returns coupled with ifs and return checking.

# Tags

- Exceptions

# Conclusion

Ids and codes are external identifiers. 

They are useful when you need to interact with an external system (for example an API Rest).

We should not use them on our own systems and our own internal APIs.

Create and raise generic exceptions.

Only create specific exceptions if you are ready to handle them, and they have specialized behavior. 

Don't create [anemic](https://dev.to/mcsee/code-smell-01-anemic-models-4dg2) Exceptions.

Avoid [immature and premature optimized languages](https://golangdocs.com/errors-exception-handling-in-golang) favoring return codes.

# Relations

{% post https://dev.to/mcsee/code-smell-26-exceptions-polluting-pbn %}

# More info

{% post https://dev.to/mcsee/how-to-get-rid-of-annoying-ifs-forever-1jfg %}

[Clean code](http://nicolecarpenter.github.io/2016/03/15/clean-code-chapter-7-error-handling.html)

# Credits

Photo by <a href="https://unsplash.com/@alex_hay">Alex Hay</a> on <a href="https://unsplash.com/s/photos/numbers">Unsplash</a>  

* * *

> Error handling is important, but if it obscures logic, it’s wrong.

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