473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What's wrong with the following code snippet.

Hi,

Please tell me what's wrong with the following code.

private bool CheckIt( int iArg, bool fArg)

{

bool fIsGood = false;

bool fIsRight;

for (int i = 20; i < 30; i++ )

{

if (i == iArg)

fIsGood = true;

}

if (fArg == true)

fIsRight = true;

else

fIsRight = false;

if (fIsGood == true && fIsRight == true)

return true;

else

return false;

}
Sep 17 '06 #1
7 1785
"Jason Kid" <Ja******@microsoft.comwrote
Please tell me what's wrong with the following code.
Can you explain what it is you're trying to do?

I looked through your code, but I was unable to puzzle out what exactly you
were trying to accomplish.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Sep 17 '06 #2
"Jason Kid" <Ja******@microsoft.comwrote in message
news:Os**************@TK2MSFTNGP04.phx.gbl...
Hi,

Please tell me what's wrong with the following code.

private bool CheckIt( int iArg, bool fArg)

{

bool fIsGood = false;

bool fIsRight;

for (int i = 20; i < 30; i++ )

{

if (i == iArg)

fIsGood = true;

}
This can be written as

fIsGood = (iArg >= 20 && iArg < 30);
>
if (fArg == true)

fIsRight = true;

else

fIsRight = false;
This can be written as

fIsRight = fArg;

but it's not needed at all.
>
if (fIsGood == true && fIsRight == true)

return true;

else

return false;

}
This can be written as

return (fIsGood && fArg);

With further optimising your entire routine can be written in one line:

private bool CheckIt(int iArg, bool fArg)
{
return (fArg && iArg >= 20 && iArg < 30);
}

Michael
Sep 17 '06 #3
Another classic example of stupid abstract certification test questions. The
multiple choice answers never include "E. None of the above, no one would be
so stupid as to write this code and if they did and your employer approves
it then it's time to find a new employer."

Jon

"Chris Mullins" <cm******@yahoo.comwrote in message
news:el**************@TK2MSFTNGP04.phx.gbl...
"Jason Kid" <Ja******@microsoft.comwrote
>Please tell me what's wrong with the following code.

Can you explain what it is you're trying to do?

I looked through your code, but I was unable to puzzle out what exactly
you were trying to accomplish.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins


Sep 17 '06 #4
Not sure, but one thing that comes to mind is that it is in serious need of
refactoring and documentation.
private const int XYZRANGE_LOW = 20;
private const int XYZRANGE_HIGH = 30;
/// <summary>
/// Validates that the provided arguments are within XYZ range.
/// If <see cref="reqFlag" /is <code>false</code>, the returned value will
always be false.
/// </summary>
/// <param name="argInRange">Value to verify as being within XYZ
range.</param>
/// <param name="reqFlag">Flag to determine whether validation returns
/// <code>true</codeif validation passes.</param>
/// <returns>Boolean result of validation.</returns>
private bool ValidateForXyz(int argInRange, bool reqFlag)
{
return reqFlag && (argInRange >= XYZRANGE_LOW
&& argInRange < XYZRANGE_HIGH);
}

Jon
"Jason Kid" <Ja******@microsoft.comwrote in message
news:Os**************@TK2MSFTNGP04.phx.gbl...
Hi,

Please tell me what's wrong with the following code.

private bool CheckIt( int iArg, bool fArg)

{

bool fIsGood = false;

bool fIsRight;

for (int i = 20; i < 30; i++ )

{

if (i == iArg)

fIsGood = true;

}

if (fArg == true)

fIsRight = true;

else

fIsRight = false;

if (fIsGood == true && fIsRight == true)

return true;

else

return false;

}


Sep 17 '06 #5
Thanks Mike,

It seems like the code snippet has a lot of space to be further optimized
intead of logic error or serious potential performance impact.

Jason

"Michael C" <no****@nospam.comwrote in message
news:eG**************@TK2MSFTNGP03.phx.gbl...
"Jason Kid" <Ja******@microsoft.comwrote in message
news:Os**************@TK2MSFTNGP04.phx.gbl...
>Hi,

Please tell me what's wrong with the following code.

private bool CheckIt( int iArg, bool fArg)

{

bool fIsGood = false;

bool fIsRight;

for (int i = 20; i < 30; i++ )

{

if (i == iArg)

fIsGood = true;

}

This can be written as

fIsGood = (iArg >= 20 && iArg < 30);
>>
if (fArg == true)

fIsRight = true;

else

fIsRight = false;

This can be written as

fIsRight = fArg;

but it's not needed at all.
>>
if (fIsGood == true && fIsRight == true)

return true;

else

return false;

}

This can be written as

return (fIsGood && fArg);

With further optimising your entire routine can be written in one line:

private bool CheckIt(int iArg, bool fArg)
{
return (fArg && iArg >= 20 && iArg < 30);
}

Michael

Sep 17 '06 #6
Michael C wrote:

if (fArg == true)

fIsRight = true;

else

fIsRight = false;

This can be written as

fIsRight = fArg;
This should generate a compiler error as fArg is an int and flsRight is
bool.

Sep 18 '06 #7
Michael C wrote:
if (fIsGood == true && fIsRight == true)

return true;

else

return false;

}
Oh, and if (fIsGood == true && fIsRight == true) is redundant.

if (fIsGood && fIsRight)
But, of course what you wrote was better:

return (fIsGood && fIsRight);

Sep 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
3
by: jwayne | last post by:
I'm trying to implement a navigation bar with multiple a href classes. The following example works exactly as I want it to, but for Internet Explorer only. Firefox does not indent each link. ...
7
by: Vic | last post by:
Dear All, I found this code snippet on this list (taken from a nice webpage of a courteous fellow), which I used to filter a form on a combo box. I wanted to repeat the same code to have an...
9
by: Eric Lilja | last post by:
Hello, I have two code snippets I want you to look at. My program compiles without warnings (warning level set to max, gcc 3.4.3) with either snippet but the latter one causes a segfault at...
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
1
by: Shapper | last post by:
Hello, I am trying to create a dataset by adding rows to it in a For loop: Dim dsNews As DataSet = New DataSet() Dim row As DataRow = dsNews.Tables(0).NewRow() dsNews.Tables.Add...
0
by: Guy | last post by:
I'm using a CreateUserWizard with an extra WizardStep. When the method "protected void CreateUserWizard1_CreatedUser(...) executes, using the debugger I can see all entered values except the...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.