Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a more elegant DO WHILE solution

Tags:

c#

do-while

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

like image 918
kurabdurbos Avatar asked Jul 18 '18 20:07

kurabdurbos


1 Answers

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.

like image 169
Nathalia Soragge Avatar answered Nov 15 '22 14:11

Nathalia Soragge