Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle un/happy paths #205

Open
szepeviktor opened this issue Jun 19, 2022 · 6 comments
Open

How to handle un/happy paths #205

szepeviktor opened this issue Jun 19, 2022 · 6 comments

Comments

@szepeviktor
Copy link
Contributor

if (error) unhappy path;

happy path;

Happy path should not be in a conditional.

Would you welcome a PR?

@peter-gribanov
Copy link
Contributor

A similar example has already been described.
https://github.com/jupeter/clean-code-php#avoid-nesting-too-deeply-and-return-early-part-2

It may be worth giving a more detailed example of the Return early pattern.
https://szymonkrajewski.pl/why-should-you-return-early/

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Jun 20, 2022

Hello Peter!
Thank you for your response.

This issue is about putting unhappy path in a conditional, and writing happy path as normal flow.

if (!error) happy path;

unhappy path;

❌ This uses Return early pattern but makes you feel that the unhappy path is the desired one.
Does it make sense?

@szepeviktor
Copy link
Contributor Author

I like Szymon's article ❤️

@peter-gribanov
Copy link
Contributor

The Return early is not only about Fail fast. It's normal to prioritize processing successful conditions over failing ones. But in practice, there are more cases in which it is more convenient to process an erroneous case first.

@szepeviktor
Copy link
Contributor Author

So what do you suggest?

@peter-gribanov
Copy link
Contributor

I see the most optimal solution to rewrite the description of Avoid nesting too deeply and return early (part 2) rule and rename it to Return early pattern. In the comments, you can mention the Fail fast pattern for linking these patterns and alternative search.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants