10

I fix antique code for a living and regularly come across code like this, and this is actually the good stuff!

Worst usecase for a goto statement? What do you think?

  int sDDIO::recvCount(int bitNumber){

  if (bitNumber < 0 || bitNumber > 15) return 0;   //ValidatebitNumber which has to be 0-15

  //Send count request

  if (!(send(String::Format(L"#{0:X2}{1}\r", id, bitNumber)) && flushTx())){

bad:   //Return 0 if something went wrong

  return 0;

}

  String^ s = recv(L"\r");   //Receive request data

  if (s->Length != 9) goto bad;   //Validate lenght

s = s->Substring(3, 5);   //Take only relevant bits

 

  int value;   //Try to parse value and send to bad if fails

  bool result = Int32::TryParse(s, value);

  if (!result) goto bad;

 

  int count = value - _lastCount[bitNumber];   //Maximumpossible count on Moxa is 65535.

  if (count < 0) count += 65536;   //If the limit reached, the counter resets to 0

_lastCount[bitNumber] = value;   //This avoids loosing count if the 1st request was

  //made at 65530 and the 2nd request was made at 5

  return count;

}

Comments
  • 1
    C++/CLR?

    And i like that it's literally useless, they're using it just once
  • 0
    @Krokoklemme yeah it was written by someone who was semi competent in C++ and had to port to C#, so for a whole bunch of code he just copy and pasted it into managed C++ and bodged it into working. He totally didn't get what the garbage collector does, amongst pretty much everything.
  • 1
    Well he DID say "goto bad"
  • 0
    @gurumeditation that's a good point actually, maybe he was much better than I give him credit for.
Add Comment