Main      Site Guide    
RinkWorks Stupid Emails

621.    One stupidity isn't so stupid

To:  sam@rinkworks.com
Subject:  One stupidity isn't so stupid
Date:  Thu, 26 Aug 2010 13:28:18 +0100

quote from  http://www.rinkworks.com/stupid/cs_programming.shtml:

        --------------------------------------------------------------------------
        I had a probationary programmer working for me. Needless to say, he
        never got to be permanent. One day I was inspecting his C code and found
        this:

        if ( a = 1 ) {
     ...some code...
        } else {
     ...some other code...
        }

        I told him the "else" clause will never get executed because of his "if"
        statement. I asked him to figure out why. He said he'd "investigate" it
        first. I allowed him to "investigate," since it had not been a critical
        task.

        A day later, he told me he figured out the problem. He said he used an
        incorrect operand in the "if" statement -- it should have been ==
        instead of =, which was absolutely correct. But then he emailed me his
        revised code.

        a = 1;
        if ( a == 1 ) {
     ...some code...
        } else {
     ...some other code...
        }

        What the...?

        I asked him if the "a = 1" part was necessary and not just a fragment of
        debug code he forgot to remove. He said it was necessary. So I asked him
        if the "else" statement would ever be executed. He said yes. I asked him
        to give me a situation when such would occur. He said he'd get back to
        me with the explanation.

        I kicked him out of the project that same afternoon.
        ---------------------------------------------------------------------------

There *is* a situation where the else statement would get executed, if
the code is operating in a multi-threaded environment, and 'a' is
accessible from another thread, and there is no locking on this chunk of
code, then 'a' could of changed by the time it gets checked.

Maybe it wasn' so stupid after all...

From:  Samuel Stoddard <sam@rinkworks.com>
Subject:  Re: One stupidity isn?t so stupid
Date:  Thu, 26 Aug 2010 09:58:04 -0400

Good thinking!  You're right, in that case there might be a race condition
which might cause the else clause to execute.  I hadn't thought of that.

But I disagree that this possibility indicates a lack of stupidity.  One, if
this situation were the one he was specifically trying to trap, he'd have been
able to answer the question when the other guy asked him to explain what that
situation was.  Two, I can't think of a single practical application -- except
as some sort of curious experiment (the results of which would only apply to
the current machine in the current environment anyway) -- to create a race
condition in multi-threaded code just to see if it ever gets trapped.  Three,
if trapping a race condition were what he was actually angling for, then his
initial code, which embeds the assignment inside the if clause, would work
equally well, as in neither case would the assignment and the conditional
check be compiled into an atomic operation.  Four, if the guy takes a full day
to figure out the difference between "=" and "==", the very concepts of a race
condition and of multi-threaded programming in general are likely beyond him.

It's a perfectly understandable and forgivable "stupidity," of course.  The
difference between = and == traps virtually every new programmer at some
point.  But I can't see how his code could possibly have been what he really
wanted it to be.

-- Sam.