4 ms·
I also don't understand this, nothing wrong about Yoda conditions and I don't understand the 'cognitive load' argument since the condition is just as readable.
by flohofwoe 10y ago
I also don't understand this, nothing wrong about Yoda conditions and I don't understand the 'cognitive load' argument since the condition is just as readable. And: Visual Studio 2015 does not warn in the case of "if (a = 5)", not even at the highest warning level, only the VS2015 static code analyser catches this.
- maccard 10y agoIt also probably shouldn't (warn) if(Foo* a = GetAFoo()) { /* Do something with a non-null foo */ } is perfectly valid code.
- gnaritas 10y agoDepends on the language, C# won't allow that, if's don't accept null, they accept bool.
- maccard 10y agoTrue. I was speaking of C++, which also accepts bool [0] (or more specifically, something that can be converted to a bool, which a null pointer can be [1]). Actually [0] has a good example of when the if (Foo* a = ...) syntax is useful (dynamic cast) [0] http://en.cppreference.com/w/cpp/language/if http://en.cppreference.com/w/cpp/language/if [1] https://ideone.com/im4uVn https://ideone.com/im4uVn
- kccqzy 10y agoI believe that's a declaration inside `if`. C doesn't support that, but C++ does. I believe in this case you can't actually put parenthesis around it.
- to3m 10y agoWith Visual Studio 2015 update 3, and /W4, I get "warning C4706: assignment within conditional expression" for stuff like "if(a=5)", both in C and C++. (C4706: https://msdn.microsoft.com/en-us/library/7hw7c1he.aspx https://msdn.microsoft.com/en-us/library/7hw7c1he.aspx)
- MaulingMonkey 10y agoI cannot generate the warning via command line flags only (including getting very insistent with /wall /w14706 /we4706 ) on VS2015 update 1, presumably a bug.
- MaulingMonkey 10y agoA non-static-analysis option: #pragma warning(error: 4706) Interestingly I cannot seem to enable this warning at all via the command line. EDIT: From bellow, possibly a VS2015 U1 bug fixed on-or-before VS2015 U3.
- Ace17 10y agoWhat is wrong with Yoda conditions? The very name is giving you a hint: they will feel natural only to Yoda. To humans, they will feel as they're written backwards.
- jusssi 10y agoHey, germans are humans too!