-
Notifications
You must be signed in to change notification settings - Fork 19
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
SIM106: Handle error cases early #8
Comments
|
|
Hi @MartinThoma! I just ran into another thing :) I often use a "else Exception" when I want to make 110% sure to avoid any definition gaps. I will quite obscurify my code if I do the (never happens) case on top with all the things that my variable might not be.
Any ideas about that? 😃 |
Oh, interesting! I had this with In this simple example, I would actually write it like this:
But I guess your real case is more complicated? Could you maybe share an example so that I can get a better understanding? |
Hi @MartinThoma - it's not more complicated I think but I really don't like creating a long list for the exit condition. Especially when the numbers are constants, which are very long to write down like |
I can see your point. As a general remark: Although I try to make this plugin "agreeable" in the sense that typically one can take the rules as they are, such cases will always come. Please take the rules as an opinionated recommendation. It is absolutely understandable if you disable rules that don't fit your general style. Specifically, in this case, I have a hard time seeing how this could become hard to read if you also use the black formatter. You can give this a name and then do this:
You can even combine this with Literal values for type checking |
@MartinThoma True, this might be the way to go. Still, I'd duplicate all choices so I doesn't feel so DRY anymore. In a regular function, I'd agree that exit conditons should go first to avoid nested if-conditions. But in an if? Why you think that it's a bad pattern? 😃 |
Thank you! I was all the time feeling that something is missing in that rule - that was it! It needs a check that it's the first thing in a function (maybe allowing few exceptions 🤔 ... hm, not sure ) |
Cool, glad I could help 😃 Another question: The other day I was trying out some new rules for the https://github.com/rocioar/flake8-django package. Do you maybe have experience with flaking django model attributes? |
Other solution: handlers = {
MyModel.SomeValueChoice.MY_FANCY_CHOICE1: do_stuff1,
MyModel.SomeValueChoice.MY_FANCY_CHOICE2: do_stuff2,
MyModel.SomeValueChoice.MY_FANCY_CHOICE3: do_stuff3,
MyModel.SomeValueChoice.MY_FANCY_CHOICE4: do_stuff4,
}
handler = handlers.get(a, lambda: ValueError(f"Invalid parameter a={a}"))
handler() I it's OK for you I'd like to contribute a pr where simple |
@alk-acezar I'm unhappy with SIM106. What do you think abou removing it altogether? Does it actually provide value? |
It's useful in some cases IMO, but I do find myself having to add |
Explanation
Exceptions and error cases should be handled early to prevent deeply-nested code.
Example
The text was updated successfully, but these errors were encountered: