Refactoring QueryHits::prepareReadPayload

Dear all

       While browsing the code, I came upon this function. Its length made me stop and take a look….

We should always look into the lib for any usable bits ( Or refactor towards them ). Here, it seems, we should be using indexOf method…

       This is what I think it should look like… I have applied Extract Method refactoring after using indexOf… It is simpler and opens doors for more refactorings ;-)

/* Refac. Note — I would like to use size_t instead of int */
/* it seems more natural */
bool lenCString( const QByteArray &rawPayload, int pos, int* len )
{
 int startPos = pos;
 pos = rawPayload.indexOf( 0, startPos );
 if( pos == -1 )
  return false;
 *len = pos - startPos;
 return true;
}

bool QueryHits::prepareReadPayload (const QByteArray &rawPayload)
{
 int payloadLength = rawPayload.length(); 
 if (payloadLength (rawPayload.at(0));

 for( int i = 0; i

Would you like to post a relpy?


This post starts a thread.
Follow-ups:
Re: Refactoring QueryHits::prepareReadPayload
Hello everybody, It's nice you brought up this topic, Atul! I think some good things can be done in regard to prepareReadPayload(). The original idea was to separate validation of packet (more...)
Re: Refactoring QueryHits::prepareReadPayload
Ouch ... I overstepped the limits... ;-) I have attached the file...    --- cheers atul