Whats Wrong With This Code

Here's an actual example from the interview guidelines of a company I once interviewed with. The game is "What's wrong with this code?"

 int A( CBar *pBar )
 {
CFoo *pFoo;

pFoo = new CFoo( pBar ); if( NULL == pFoo ) return( -1 );

if( !pBar->Validate() ) delete pFoo;

return( pFoo->GetValue?() ); }

// where there is a constructor for Cfoo: CFoo::CFoo( CBar * );
I was told the given correct answer was that (to summarize) the method should be written:

 int A( CBar *pBar )
 {
CFoo *pFoo;

pFoo = new CFoo( pBar ); if( NULL == pFoo ) return( -1 );

if( !pBar->Validate() ) { delete pFoo; return -1; }

return( pFoo->GetValue?() ); }
But do you see what else is wrong? Ordered from most innocuous to most ludicrous:

Other criticisms?

(*) Further, we have the following question

Write StrCpy?() where the prototype is,

uint strcpy( char *pDst, char *pSrc );

which is bogus on many levels. Ignoring the BadVariableNames, the correct signature is

char *strcpy( char *pDestination, char const *pSource );

But later on, the question appears

What does "const" mean?
which leads me to believe the programmers at this company don't know enough C++ to conduct the interview, let alone do their jobs. Actually, to be fair, the interview has some good questions relative to the rest of the interviews with code I've seen, but that only depresses me further. -- SunirShah


"Ignoring the BadVariableNames" how can you do that?

My first reply would be "these variable suck". To quote MartinFowler: "Any fool can write code that a computer can understand, Good programmers write code that humans can understand. "

MetasyntacticVariables are always given as examples in computer books, but I think it's a dangerous practice. It implies tacit validation that this code is 'proper', after all, some hot-shot programmer got paid to write the book.


What does "const" mean?
Hmmm. Would you consider Discuss the pros and cons of using const a better question? Although, it may wind up being a filter, selecting those who have read ScottMeyers from those who haven't...


EditText of this page (last edited November 2, 2011) or FindPage with title or text search