Factored Out Some Magic Numbers

GeneralDev.jpg

One of the things I really hate about "sloppy" code is the use of Magic Numbers in the code. Things that look like this:

  if (mAutoFlipSize < 400000) {
    if (((++emptyTrips > 75) && (sz > 50)) ||
        (sz > mAutoFlipSize)) {
      drainAllPendingMessages();
      flipSide();
      continue;
    }
  }

Unfortunately, this is all my code. I have no one to blame but myself. I started tuning this code, and needed to play with the buffer sizes and limits, and this is what I was left with.

But having it, and leaving it are two entirely different things. I spent a few minutes today to remove all these magic numbers and use a simple grouped enum to make them far more manageable:

  namespace msg {
  namespace kit {
  namespace udp {
  enum tConst {
    eAutoFlipEmptyTripSize = 50,
    eAutoFlipEmptyTrips = 75,
    eAutoFlipDefault = 50000,
    eAutoFlipManual = 400001,
  };
  }     // end of namespace udp
  }     // end of namespace kit
  }     // end of namespace msg
 
 
  if (mAutoFlipSize < udp::eAutoFlipManual) {
    if (((++emptyTrips > udp::eAutoFlipEmptyTrips) &&
         (sz > udp::eAutoFlipEmptyTripSize)) ||
        (sz > mAutoFlipSize)) {
      drainAllPendingMessages();
      flipSide();
      continue;
    }
  }

Much better! I now know that I can keep the constants in sync with the code. No more Magic Numbers.