this post was submitted on 09 Jun 2025
90 points (95.0% liked)

Programmer Humor

36519 readers
243 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 4 days ago* (last edited 4 days ago) (1 children)

Prefix the name with what it's for. For example, I've previously got a SoundFontError from opening soundfont file.

"Error" is already used by std::error::Error. It might not be imported by the code that imports your error type, but I think it's better to give it distinct name.

The other thing is that you might want to use more than one library. Typical imports at the top of the file might look like this:

use bingbong::{BingBong, BingBongError, SomethingElse};
use bogos::binted::crotchidizer::{Crotchidizer, CrotchidizerError};

If both libraries named their error enums just "Error", the collision could be worked around, but it's an unnecessary extra step:

// Not sure how renaming affects compiler hints.
use bingbong::{BingBong, Error as BingBongError, SomethingElse};
use bogos::binted::crotchidizer::{Crotchidizer, Error as CrotchidizerError};

or if you want to avoid renaming:

use bingbong::{BingBong, SomethingElse};
use bogos::binted::crotchidizer::{self, Crotchidizer};

/* ... */

    match result {
        Ok(value) => return value,
        Err(bingbong::Error::MissionFailed) => panic!(),
        Err(bingbong::Error::UrMom) => todo!(),
        _ => unreachable!(),
    }
    
    if let Err(crotchidizer::Error::SomethingsWrong) = result2 {
        // ...
    }
    

If the screenshot had followed conventions, the message would say something like this:

could not convert error type `BingBongError` to `MyAppError`
[โ€“] [email protected] 1 points 2 days ago

I see, thanks for the time you took explaining this. I think it is more a cultural difference then in how patterns evolved.