[comp.lang.c] C test

mc68020@gilsys.UUCP (Thomas J Keller) (07/31/87)

   I recently posted regarding the nasty little pointer problem I encountered
on a "programming test" popped on me by a job shop.  Here is another of the
little gems from that test.  I just *LOVE* this one!

Improve the following code fragment through reorganization:

{
    if (A) { B; return; }
    if (C) { D; return; }
    if (E) { F; return; }
    G; return;
}


   Cute, eh?  The thing that's ironic about it is that this is typical of the
quality and scope of the specifications I have been handed by employers for
PRODUCTION coding!  No wonder to me that there are so many buggy, slow, pain-
in-the-arse programs out there!

-- 
Tom    : The conservatives always grouse about "Law & Order" when the liberals
Keller : break the law...when the Reagan Admin. does it, it's PATRIOTISM!

UUCP   : {ihnp4,ames,qantel,sun,amdahl,lll-crg,pyramid}!ptsfa!gilsys!mc68020
BITNET : ptsfa!gilsys!mc68020@ames.com

decot@hpisod2.HP.COM (Dave Decot) (08/02/87)

> Improve the following code fragment through reorganization:
> 
> {
>     if (A) { B; return; }
>     if (C) { D; return; }
>     if (E) { F; return; }
>     G; return;
> }

I have several answers to this; each would be the "right" answer depending
on who is asking the question and what hidden agenda they have.

1) Since we do not know whether the definitions, if any, of A, B, C, etc.,
   contain imbalanced syntactic tokens, there is no way to change this
   fragment without possibly breaking the code.

2) {
	if (A)
	    B;
	else if (C)
	    D;
	else if (E)
	    F;
	else
	    G;

	return;		/* If the braces enclose a complete function definition
			   rather than a nested block, this statement should
			   be deleted. */
   }

3) This is for the APL freak; it assumes B, D, F, G are expressions:

   { A?B : C?D : E?F : G; return; }

I suppose my best answer would be 2, and then 1, and then 3 (if I thought
the person was probably psychotic).

Dave Decot
hpda!decot

ron@topaz.rutgers.edu (Ron Natalie) (08/04/87)

{
    if (A) { B; return; }
    if (C) { D; return; }
    if (E) { F; return; }
    G; return;
}

is efficiently replaced by

{ return; }

since the computed values B, D, F, and G are ignored.  Unless, the
symbols A through G are supposed to indicate arbitrary expressions
that have side effects such as assignments or function calls in them.

-Ron

scott@applix.UUCP (Scott Evernden) (08/04/87)

In article <1090@gilsys.UUCP> mc68020@gilsys.UUCP (Thomas J Keller) writes:
>Improve the following code fragment through reorganization:
>{
>    if (A) { B; return; }
>    if (C) { D; return; }
>    if (E) { F; return; }
>    G; return;
>}

An improvement?: 
{
	A ? B : C ? D : E ? F : G;
}
which generates exactly the same code on my sun.

-scott

john@frog.UUCP (John Woods, Software) (08/08/87)

In article <2550025@hpisod2.HP.COM>, decot@hpisod2.HP.COM (Dave Decot) writes:
> > Improve the following code fragment through reorganization:
> > {
> >     if (A) { B; return; }
> >     if (C) { D; return; }
> >     if (E) { F; return; }
> >     G; return;
> > }
> I have several answers to this; each would be the "right" answer depending
> on who is asking the question and what hidden agenda they have.
> 1) Since we do not know whether the definitions, if any, of A, B, C, etc.,
>    contain imbalanced syntactic tokens, there is no way to change this
>    fragment without possibly breaking the code.
> 2) {	if (A)	    B;
> 	else if (C) D;
> 	else if (E) F;
> 	else 	    G;
> 	return;
>    }
> 3) <for the APL freak in all of us...>

To me, the correct answer is:
4) "That's not my job.  That's the job of the compiler writer."

Although the lettered code blocks aren't given, when I assume them to be
function calls, the Greenhills C compiler generates identical code for the
original and for the classical reorganization given.

Think of this test not so much as a test of your abilities, as a test of
whether or not you want to work for the clown that asked it...

--
John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, ...!mit-eddie!jfw, jfw%mit-ccc@MIT-XX.ARPA

"The Unicorn is a pony that has been tragically
disfigured by radiation burns." -- Dr. Science

karl@haddock.ISC.COM (Karl Heuer) (08/11/87)

In article <1545@frog.UUCP> john@frog.UUCP (John Woods, Software) writes:
>> > Improve the following code fragment through reorganization: [...]
>To me, the correct answer is:
>4) "That's not my job.  That's the job of the compiler writer."
[and goes on to say that his compiler generates identical code]

Fine, if you interpret "improve" to mean "optimize the generated code".  I
interpreted it as "improve the *style*", which *is* the programmer's job.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint