Looking for a more elegant DO WHILE solution

  • A+
Category:Languages

So I have some code centered around a do while loop.

string token; var count = 0;       var checkDataLength= data.Length == 16;  do {                   count = 0;     token = GenerateToken(data, start, end);      if (checkDataLength)     {         if (Mod120Check(token))         {                                   count += 1;         }     }      if (IsCompliant(token))     {         count += 1;     } }  while (count > 0);  return token; 

Basically, I am generating a token and for this token to be valid, has to FAIL the Mod120Check and the IsCompliant check. I cannot change how those methods return the data.

While the above code works, I feel that its ugly and I was wondering if anyone had a better way of doing it?

Thanks

 


Try this:

do {     token = GenerateToken(data, start, end); }  while (checkDataLength && Mod120Check(token) || IsCompliant(token)) 

Just moving your conditions to the while.

(!) Notice that IsCompliant(token) will only be called if checkDataLength && Mod120Check(token) states false. It shouldn't cause any colateral effects, but it could, depending on what your IsCompliant method does.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: