- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
frist: as it is written, in the null check this.mode is assigned null secnod: mode always evaluates to invalid
Admin
Another WTF: nothing is done with the default value 4. Why don' t they set this.mode to 4 when the input is null?
Admin
if (mode == null) { this.mode = mode; return ; }
^^^ THAT'S your WTF!!!!! ^^^
Admin
Two points here:
If by "and" here, you meant
&&
, then I sure as hell hope they didn't understand that it's commutative, because&&
isn't commutative. Sure, the overall value doesn't depend on the order of the two values, but there's a difference between the behaviour off(params) && g(params)
andg(params) && f(params)
, so the order matters, and so the programming operation isn't commutative. (Yes, I agree that the fundamental logic operation is commutative, but&&
does shortcut evaluation, so the programming operation is not.)The correct thing to complain about is not "commutative" anyway, but rather "associative". All those extra parentheses indicate a failure to understand that the logic operation of
&&
is associative, that is, that(a && b) && c
anda && (b && c)
anda && b && c
are equivalent.Admin
OK, so I normally say "if you can't remember the operator precedence rules for the language you're using this afternoon, FFS do not look them up, just put the brackets in. Because of the next poor sod who's going to read your code and who also doesn't remember the operator precedence rules."
But ... surely ... "shortcut logical operators are left associative" applies across all languages, so doesn't need spelling out?
Admin
Ah yes, the ancient times when Java didn't have enums, and the fight to be allowed to fix that in today's age.
Admin
Optional is specifically intended as a RETURN type, not a PARAMETER type. Proof by authority: Brian Goetz.
https://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type/26328555#26328555
Admin
How about the WTF of not using a simple arithmetic check, since the valid values are a contiguous set of integers.
Admin
Short-circuiting order of evaluation is irrelevant when the conditions have no side effects, like simple equality conditions. But you're right, this is an issue of associativity, not commutativity.
Admin
That did occur to me, about two milliseconds after I pressed "Submit". Oh well.
Admin
And there I just wondered why people would need the Delta nuget to handle etags over DB queries. Turns out some developers don't even understand basic boolean logic these days, not to mention that there are more conditional operators beyond equality checks.
Admin
Order of evaluation still matters in short-circuiting without side effects. Consider the case where the null-check would be included in the if statement. It then needs to come first, otherwise the next check (code!= 1) will fail with an NPE.
Admin
I'd wager this wasn't written like this by hand, but rather the result of an IDE overzealously parenthesizing when doing some refactoring oppression and the dev just not bothering to fix it – got example iirc netbeans will sometimes give you something kind of like this when you have a bunch of chained ||s and hit "invert if"
Admin
for* example
Admin
VB6 and VB.Net don't have short-circuit evaluation (unless you use AndAlso instead of And and OrElse instead of Or)
Admin
Throwing an exception is a side effect (i.e. something other than returning a value).
Admin
Well they are now, but that doesn't mean they always were or always will be.
Furthermore, this mode thing doesn't look like it is meant to have the semantics of a number but numbers are merely used to label different modes. I'd feel uneasy about using properties of numbers on a type that really should be encoded as an enum.