Magic of if...else...programming
-
The following code was found in someone's web template. Hope you enjoy the best logic.
if(dt != null)
{
if(dt.Rows.Count != 0)
{
if(dt.Rows[0]["Number"].ToString() == "1")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
} -
The following code was found in someone's web template. Hope you enjoy the best logic.
if(dt != null)
{
if(dt.Rows.Count != 0)
{
if(dt.Rows[0]["Number"].ToString() == "1")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}Zzzzz... Half (or nearly) the posts on this forum are like that.
-
The following code was found in someone's web template. Hope you enjoy the best logic.
if(dt != null)
{
if(dt.Rows.Count != 0)
{
if(dt.Rows[0]["Number"].ToString() == "1")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}Oddly enough, I just got rid of some code that looked a bit like that...
bool InitRoutine()
{
bool failed=false;ValType val; HRESULT ans = GetValue1(val); if (ans==S\_OK) { globalVal1 = val; } else { failed = failed || true; } ans = GetValue2(val); if (ans==S\_OK) { globalVal2 = val; } else { failed = failed || true; } ans = GetValue3(val); if (ans==S\_OK) { globalVal3 = val; } else { failed = failed || true; } ans = GetValue4(val); if (ans==S\_OK) { globalVal4 = val; } else { failed = failed || true; } return !failed; }
-
The following code was found in someone's web template. Hope you enjoy the best logic.
if(dt != null)
{
if(dt.Rows.Count != 0)
{
if(dt.Rows[0]["Number"].ToString() == "1")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}This code reminds me one developer working under me, according to her every if has to have else, you can't use if alone. :-)
-
The following code was found in someone's web template. Hope you enjoy the best logic.
if(dt != null)
{
if(dt.Rows.Count != 0)
{
if(dt.Rows[0]["Number"].ToString() == "1")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}What's so bad about that? I'd do it like the following but I've seen far worse than that code:
// Only proceed id if 'dt' is valid and contains at least one row.
if (dt == NULL)
return false;
if (dt.Rows.Count == 0)
return false;
return (t.Rows[0]["Number"].ToString() == "1"); -
What's so bad about that? I'd do it like the following but I've seen far worse than that code:
// Only proceed id if 'dt' is valid and contains at least one row.
if (dt == NULL)
return false;
if (dt.Rows.Count == 0)
return false;
return (t.Rows[0]["Number"].ToString() == "1");How would you think of this?
return dt != null && dt.Rows.Count > 0 && (int)t.Rows[0]["Number"] == 1;
The last part depends on the data the table contains. But if its clear that the column is filled with integers then this should be more efficient.
-
How would you think of this?
return dt != null && dt.Rows.Count > 0 && (int)t.Rows[0]["Number"] == 1;
The last part depends on the data the table contains. But if its clear that the column is filled with integers then this should be more efficient.
Nah. This is much more difficult to debug, how would you set a breakpoint anywhere inside such a complex expression?
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Nah. This is much more difficult to debug, how would you set a breakpoint anywhere inside such a complex expression?
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Why would you need to? I never have. And for that purpose you can write it specifically for debugging and then put it back to "normal" once you're satisfied with that bit (and not with conditional compiling). I had to do that sort of thing once a week or so ago. It works for me, others may choose other paths.
-
How would you think of this?
return dt != null && dt.Rows.Count > 0 && (int)t.Rows[0]["Number"] == 1;
The last part depends on the data the table contains. But if its clear that the column is filled with integers then this should be more efficient.
Right, that's how I do it. Though it may be more a matter of readability/maintainability than of efficiency (in my opinion).
-
This code reminds me one developer working under me, according to her every if has to have else, you can't use if alone. :-)
-
Why would you need to? I never have. And for that purpose you can write it specifically for debugging and then put it back to "normal" once you're satisfied with that bit (and not with conditional compiling). I had to do that sort of thing once a week or so ago. It works for me, others may choose other paths.
I'm afraid you overlooked the little joke icon; I seldom set a breakpoint so I will not break up things that belong together just to facilitate a potential later debug action. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
I'm afraid you overlooked the little joke icon; I seldom set a breakpoint so I will not break up things that belong together just to facilitate a potential later debug action. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Oh, yes, indeed I did, thanks. It's because the coffee was still dripping. We had a power outage this morning so the coffee maker didn't start automatically. I'd better get a UPS for it.
-
Yeah Its been two years. I asked the HR department to improve filtering & recruitment process but as they are looking for cheap labor I don't think this will ever happen.
-
Nah. This is much more difficult to debug, how would you set a breakpoint anywhere inside such a complex expression?
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
If the statement fails, just put a breakpoint on the entire statement. Then, highlight each portion, right click, and select 'Add to Watch'. Viola! You'll see whether or not the statement passed. Rinse and repeat.
-
If the statement fails, just put a breakpoint on the entire statement. Then, highlight each portion, right click, and select 'Add to Watch'. Viola! You'll see whether or not the statement passed. Rinse and repeat.
And yes...I know it was a joke. But still...not sure if everyone knows of such ;)
-
If the statement fails, just put a breakpoint on the entire statement. Then, highlight each portion, right click, and select 'Add to Watch'. Viola! You'll see whether or not the statement passed. Rinse and repeat.
Thank you for the tip; I rarely use those fancy debug features. It does widen the scope of the breakpoint, hence requires more human intervention to narrow it down, but it works. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
And yes...I know it was a joke. But still...not sure if everyone knows of such ;)
I certainly didn't, and I probably won't the next time I could use it.
-
Oddly enough, I just got rid of some code that looked a bit like that...
bool InitRoutine()
{
bool failed=false;ValType val; HRESULT ans = GetValue1(val); if (ans==S\_OK) { globalVal1 = val; } else { failed = failed || true; } ans = GetValue2(val); if (ans==S\_OK) { globalVal2 = val; } else { failed = failed || true; } ans = GetValue3(val); if (ans==S\_OK) { globalVal3 = val; } else { failed = failed || true; } ans = GetValue4(val); if (ans==S\_OK) { globalVal4 = val; } else { failed = failed || true; } return !failed; }
Ah a follower of the 'there must be only one return statement per method cult'.
I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
BizSquawk -
Yeah Its been two years. I asked the HR department to improve filtering & recruitment process but as they are looking for cheap labor I don't think this will ever happen.
-
Ah a follower of the 'there must be only one return statement per method cult'.
I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
BizSquawkA misguided one, gives the rest of us a bad name. Though I don't see where any other
return
s would go.