Improper Default Behavior
-
So, my brain is totally sludge right now.
I just can't tell why:
-
@xaade You're assigning to
!cfg_data
? Think you missed some parentheses.
I'd also say you missed some common sense in deciding to use the 'if (x = stuff)' stuff construct in the first place, but I can see why you'd be tempted to do so.
Better languages have 'if let x = stuff' to make this sorta thing normal.
-
@xaade If check_valid fails on an exception and returns true, it seems check_valid is a fucked up function that is lying.
-
Yeah, I get that it's screwy.
I just don't get how the tutorial suggests that the two codes will have different outcomes. I think they made a typo or something. But if the outcomes are different and I'm just blind or don't understand something, I was curious if anyone else understood it differently.
I don't see any scenario where the first code would skip GetConfigData if check_valid returned true...
-
@xaade
Theif (!cfg_data = ...)
is suspicious. Many compilers will complain about missing parenthesis there.I also think the
else
in the 'incorrect' case is laid out weirdly. Even apart from the inconsistent braces, with theif
always returning from the function, theelse
should either envelop the entire rest of the function, or not be there at all (like in the 'correct' case, which still suffers from inconsistent layout).
-
Why isn't this in Sidebar or WTF Bites?
-
Is
!cfg_data = GetConfigData (input_data)
a syntax error on an invalid left-hand side for the assignment?The only other thing I notice is that the "Incorrect" code uses
!
, but the "Correct" code uses== null
.None of that seems relevant to the text in that bubble, though.
-
@anotherusername The "instructor" claims that the incorrect code leaves a possibility for the GetConfigData to be skipped but somehow manage to get to DBAccess.
I don't see it.
-
@xaade said in Improper Default Behavior:
@anotherusername The "instructor" claims that the incorrect code leaves a possibility for the GetConfigData to be skipped but somehow manage to get to DBAccess.
I don't see it.
Likely, he's just copy-pasta from the textbook. At one point, the textbook did indeed have an example that the words that went along with it that matched. But along came a smartypants, and "fixed" the code a bit without reading context of the snippet, and this is what we get.
-
@Zecc said in Improper Default Behavior:
Why isn't this in Sidebar or WTF Bites?
Because
@xaade said in Improper Default Behavior:
I just don't get how the tutorial suggests that the two codes will have different outcomes.
I presume.
-
@xaade said in Improper Default Behavior:
@anotherusername The "instructor" claims that the incorrect code leaves a possibility for the GetConfigData to be skipped but somehow manage to get to DBAccess.
I don't see it.
No, I don't think that could happen either. The "correct" version looks nicer, but after extrapolating around the typos/missing parentheses in the "incorrect" one, I too think that they are equivalent.
The exception thing is BS. Either
check_valid()
returns true, or it returns false, or it exits via an exception, which will also cause the scope of the example to be left (since there is nothing to catch the exception).(Meh, fuck, to all wannabe-instructors out there: running a <10 line code example a few times is not too much to ask for, right? I mean, most of the code snippets I post here, I compile+run before daring to post...)
-
@PJH My question was semi-rhetorical, if that's a thing.
-
@Zecc said in Improper Default Behavior:
@PJH My question was semi-rhetorical, if that's a thing.
Your post got flagged (presumably to get the thread moved,) which is the only reason I bothered replying.
-
@cvi said in Improper Default Behavior:
The exception thing is BS. Either check_valid() returns true, or it returns false, or it exits via an exception, which will also cause the scope of the example to be left (since there is nothing to catch the exception).
Strictly,
check_valid()
could also never return… but then there's no problem from the following code as it won't run at all. ;)
-
@dkf True, didn't think of that option. :-O
(Hopefully it would be marked as
[[noreturn]]
, allowing the compiler to eliminate all following code.)
-
@cvi said in Improper Default Behavior:
@dkf True, didn't think of that option. :-O
(Hopefully it would be marked as
[[noreturn]]
, allowing the compiler to eliminate all following code.)Possibly not returning is not the same as always not returning.