Skip to content

Add error handling for methods without a return value #112

Closed
@june128

Description

@june128

"error handling" means "everything to make errors possible (error types, checking for errors and so on)" in this issue. This makes formulations easier.

⭐️ Proposed change

Add error handling for methods, which don't have a return value.

New emojis, which indicate that error handling is going on, would make error handling for these methods unmistakable.

Reference: algorithm-archivists/algorithm-archive#471 (comment)

🤔 Rationale

Imagine the following two methods:

📗 Increase someNumber by the specified value. 📗
❗️ 🆙 summand 🔢 🍇
    someNumber ➕ summand ➡️ 🖍someNumber
🍉

📗 Return someNumber multiplied with the specified multiplier. 📗
❗️ ⏺ multiplier 🔢 ➡️ 🔢 🍇
    ↩️ someNumber ✖️ multiplier
🍉

Now - for the rationale - add some error handling:
Let us imagine that we don't want the methods to accept negative values and that an error should occur then.

Error Handling for the Second Method

For the second method we can just use the inbuilt error handling:

  • Create an error enumeration:
🦃 ⏹ 🍇
    🔘 🔽

    ❗️ 🔡 ➡️ 🔡 🍇
        ↪️ 🐕 🙌 🆕⏹🔽❗️ 🍇
            ↩️ 🔤The provided value can't be negative.🔤
        🍉
        ↩️ 🔤🔤
    🍉
🍉
  • Implement the error handling for the method:
📗
    Return someNumber multiplied with the specified multiplier.
    Only allow positive multipliers to have a reason to drop an error.
📗
❗️ ⏺ multiplier 🔢 ➡️ 🚨⏹🔢 🍇
    ↪️ multiplier ◀️ 0 🍇
        🚨🆕⏹ 🔽❗️
    🍉
    ↩️ someNumber ✖️ multiplier
🍉

After we have done this, we can call the method and handle potential errors the following way:

💭 Here the method ⏺ doesn't return an error.
🥑 returnValue ⏺ myTest 10❗️ 🍇
    😀 🔡 returnValue 10❗️❗️
🍉
🙅‍♀️ error 🍇
    😀 🔡error❗️❗️
🍉

💭 Here the method ⏺ does return an error.
🥑 returnValue ⏺ myTest -1 ❗️ 🍇
    😀 🔡 returnValue 10❗️❗️
🍉
🙅‍♀️ error 🍇
    😀 🔡error❗️❗️
🍉

Error Handling for the First Method

For the first method (which doesn't have a return value) we can't use the inbuilt error handling - we need to use optionals to mimic error handling.

This can be done in the following way:

  • Use the same enumeration as above.
  • Mimic error handling by using an optional:
📗
    Increase someNumber by the specified value.
    Only allow positive summands to have a reason to drop an error.
📗
❗️ 🆙 summand 🔢 ➡️ 🍬⏹ 🍇
    ↪️ summand ◀️ 0 🍇
        ↩️ 🆕⏹🔽❗️
    🍉
    someNumber ➕ summand ➡️ 🖍someNumber
    ↩️ 🤷‍♀️
🍉

Now we can check for errors by evaluating whether or not the optional holds a value:

💭 Here the method 🆙 doesn't return an error.
↪️ 🆙 myTest 10❗️ ➡️ return 🍇
    😀 🔡return❗️❗️
🍉

💭 Here the method 🆙 does return an error.
↪️ 🆙 myTest -1❗️ ➡️ return 🍇
    😀 🔡return❗️❗️
🍉

Conclusion

Even tho we can mimic error handling for methods without a return value, we need to do so using optionals. This isn't perfect for a few reasons:

  • Having the return value be an optional doesn't necessarily mean that error handling is mimicked. Therefore the (mimicked) error handling isn't identifiable from the methods signature.
  • With the error handling for methods with a return value, the 🥑 clearly indicates an error check. For methods without a return value, checking for the error is just evaluating, if the optional holds a value. It isn't clear that this particular check is a check for an error.
  • We also can't use 🚥 for methods without a return value.

🕺Example

Provided in the Rationale.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions