Which one is better [modified]
-
//Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One
bool IsOk=false;
while(i < 1000)
{
………….
…………//IsOk may change here
………….
if(i< 100)
{
IsOk=false;
}
i++;
}Second
bool IsOk=false;
while(i < 1000)
{
………….
………… //IsOk may change here
………….
if(IsOk && i< 100)
{
IsOk=false;
}
i++;
}Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
modified on Monday, September 27, 2010 4:58 AM
What language is that?
-
//Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One
bool IsOk=false;
while(i < 1000)
{
………….
…………//IsOk may change here
………….
if(i< 100)
{
IsOk=false;
}
i++;
}Second
bool IsOk=false;
while(i < 1000)
{
………….
………… //IsOk may change here
………….
if(IsOk && i< 100)
{
IsOk=false;
}
i++;
}Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
modified on Monday, September 27, 2010 4:58 AM
-
Maybe it involves a costly network or database transaction?
-
//Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One
bool IsOk=false;
while(i < 1000)
{
………….
…………//IsOk may change here
………….
if(i< 100)
{
IsOk=false;
}
i++;
}Second
bool IsOk=false;
while(i < 1000)
{
………….
………… //IsOk may change here
………….
if(IsOk && i< 100)
{
IsOk=false;
}
i++;
}Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
modified on Monday, September 27, 2010 4:58 AM
They both suck. Isok seems to be handling two conditions. The one hidden in the comments, that could change it. And the less than 100. That's confusing. In any case You don't need an if. Isok = I < 100 Would work. Sorry for caps and formatting, I'm on a phone. Rd
-
Maybe it involves a costly network or database transaction?
-
yes you got it so what will you prefere?
Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
Even if it does involve an expensive operation, with the code structure we can see, i still cannot see how it makes a difference. If the code before the 'if' checks the isOk to perform the operation then maybe, but if isOK is already true, and you need to set it back to false if i<100, then maybe the whole way the operation is being done need to be reviewed. If your logic requires for the the operation to continue until i > 100, then it doesn't matter what happens with isOK, or if it is true already. just set it back to false and get on with what you need to do. I am maybe not explaining very well what i'm trying to say, but i hope you catch my drift.
Dave Find Me On: Web|Facebook|Twitter|LinkedIn CPRepWatcher now available as Packaged Chrome Extension, visit my articles for link.
-
//Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One
bool IsOk=false;
while(i < 1000)
{
………….
…………//IsOk may change here
………….
if(i< 100)
{
IsOk=false;
}
i++;
}Second
bool IsOk=false;
while(i < 1000)
{
………….
………… //IsOk may change here
………….
if(IsOk && i< 100)
{
IsOk=false;
}
i++;
}Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
modified on Monday, September 27, 2010 4:58 AM
At the moment I'm more concerened with the meaning of
while(int i=0)
. Will it compile? Will it execute the loop? -
At the moment I'm more concerened with the meaning of
while(int i=0)
. Will it compile? Will it execute the loop?PIEBALDconsult wrote:
Will it compile? Will it execute the loop?
Yes. No.
-
//Updated Hello everyone I just want to know cost of Assignment statement Isok = false; and if condition if(Isok == true) Isok=false; Thanks First One
bool IsOk=false;
while(i < 1000)
{
………….
…………//IsOk may change here
………….
if(i< 100)
{
IsOk=false;
}
i++;
}Second
bool IsOk=false;
while(i < 1000)
{
………….
………… //IsOk may change here
………….
if(IsOk && i< 100)
{
IsOk=false;
}
i++;
}Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
modified on Monday, September 27, 2010 4:58 AM
At first, I thought the coding horror was the "while(int i = 0)" (or whatever it was), but I didn't understand why you posted two similar versions. Now that you changed it, I don't understand what the coding horror is at all.
-
At first, I thought the coding horror was the "while(int i = 0)" (or whatever it was), but I didn't understand why you posted two similar versions. Now that you changed it, I don't understand what the coding horror is at all.
Maybe the coding horror is premature optimisation? Unless the programmer has determined that this is a bottleneck, the better one is the one that expresses the logic most clearly (the one without the extra test, for my money), not the one with the lowest "cost". And if the programmer has determined that this is a bottleneck, they can use the same tools to find out which is faster.