-
Notifications
You must be signed in to change notification settings - Fork 39
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
Prefer Yoda Conditionals #163
Comments
I strongly disagree with the whole yoda notation style. Its origin reads like a joke (after wickedpedia):
These are also an indication that this particular style hasn't been used widely within any community, except the two, from which the WordPress should be treated IMO as an anti-advertisement - as the code quality there was always disastrous. Also, see the Criticism section on the above mention page. |
The criticism section is incredibly thin. There is an unverified claim to be "widely criticized". It only references three random blog posts that may proof that some random people advise against using it. One of those doesn't give any reasoning, the others make their arguments on questionable assumptions and generalizations and in the scope of specific programming environments. Rest of the section talks about specifics of some programming languages and I understand it mostly as removing some of the reasons for Yoda notation in these languages. I don't see anything near a convincing argument that Yoda notation is bad. That's just an observation of the Wikipedia reference. Maybe there are convincing arguments, but they're not convincingly expressed in that article nor is it backed by substantial evidence. |
I have my own argument that Yode notation can be benefital in some cases. if LibFoo.generate_foo_bar_baz(foo, pointerof(bar).as(Void*), baz.to_slice, LibFoo::FooTypes::BAR_BAZ, LibFoo.some_other_thing) == 0
if 0 == LibFoo.generate_foo_bar_baz(foo, pointerof(bar).as(Void*), baz.to_slice, LibFoo::FooTypes::BAR_BAZ, LibFoo.some_other_thing) I believe the Yoda style actually reduces cognitive load here. Of course, this can be written differently. Long method calls could be split over multiple lines. That doesn't make a difference. Yoda notation is IMO also better than a if LibFoo.generate_foo_bar_baz(
foo,
pointerof(bar).as(Void*),
baz.to_slice,
LibFoo::FooTypes::BAR_BAZ,
LibFoo.some_other_thing
) == 0
if 0 == LibFoo.generate_foo_bar_baz(
foo,
pointerof(bar).as(Void*),
baz.to_slice,
LibFoo::FooTypes::BAR_BAZ,
LibFoo.some_other_thing
) Yoda definitely reads better here. Sure you could store the return value in a local variable and use that like |
I don't see anything near a convincing argument that Yoda notation is bad. @straight-shoota I don't see any convincing argument it's actually beneficial, sorry. This only argument being accidental assignment avoidance is incredibly thin, using your words - throughout multiple yrs I could count perhaps a dozen of such cases, in my code or in others'.
I'm sorry but, where is substantial evidence that using yoda notation does actually any good? We have none, yet. That should be included by the OP in the first place.
Your argument is based on the fact that the line is long enough you won't scan the ending of it, but as you already noticed the 1st and foremost issue here is the line/expression length.
I'm not sure what cognitive load you have in mind while talking about |
TBH I think you're exaggerating how big of a deal this is. I don't see how If this ever gets implemented, just make it disabled by default and let the user decide for themselves versus trying to shut it down just because you disagree with the style/its benefit. |
@Blacksmoke16 It's written backward - that's more challenging. If you want the same argument from elsewhere - quoting the wickedpedia page:
in re:
That's cool with me, although I wouldn't like to have it enabled by default, nor promoted in any way. |
It's similar to the multi-line expression with |
@straight-shoota When you have a line that long, you have more serious problems than whether to write it yoda-style or not and doin' so won't fix 'em, anyway so that's a moot point for me. |
I'm not talking about a long line, but a long expression over multiple lines. |
It's an edge-case to see multi-line expression with equality check, so doesn't seem like a convincing argument either. |
I'm closing this issue, since there's no consensus over the validity of such rule. |
Just my 2 cents: I personally wouldn't use this rule, but there is at least some interest in it. So it might make sense as an optional? Would still need somebody to implement it, of course. However, I also have a superior proposal for avoiding unintended assignments in #294. |
https://en.wikipedia.org/wiki/Yoda_conditions
This compiles (and works), but is most likely a typo. If yoda conditionals were prefered this would turn into a compile time error.
The text was updated successfully, but these errors were encountered: