Magic of if...else...programming
-
Other than what has been already pointed out..."code trimming" for lesser cycles, I also suggest not to use the string literals as such. They break the clients when modified. static readonly string colName = "Number"; static readonly string colVal = "1"; -------------------------- if(dt != null && dt.Rows.Count > 0) { return dt.Rows[0][colName].ToString().Equals(colVal); } else return false;
vnike wrote:
They break the clients when modified.
Only if the client is a different assembly. I would imagine that column names and indexes would be private to a class, so I don't see any issue with using const there.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Beautiful... failed = failed || true;
Short circuit evaluation, man! You don't want to be setting failed to true if it is already! :)
-
Nice (and compact) solution !! Still depends on the precedence priorities of the language / the optimization of the underlaying compiler .. More safe and maintainable code: ------------------------------------ bool res = false; if (null == dt) else if (null == dt.Rows) else if (dt.Rows.Count < 0) else res = (1 == (int)dt.Rows[0]["Number"]); return res; ------------------------------------ Rules to be applied : (1) : prevent against '=' instead of '==' : always put constants first (2) : always control potential nulls even if seems useless versus construction rules (ex null == dt.Rows) (3) : provide debugging / tracing points in case of future problems (4) : write readable code (5) : single return output point Shears and happy new year.
modified on Saturday, January 10, 2009 5:03 AM
would the same work for a switch statement?
switch (null)
{
case dt:
{} break;
case dt.rows:
{} break;
default:
{/*actual exec., we have asserted everything's cool*/}
}or how about this?
switch (true)
{
case (dt == null):
{/*handle the case that dt is null,
i.e. assign it something*/} break;
case (dt.rows == null):
{/*handle the case that dt.rows is null*/} break;
case (dt.rows[0]["number"] == 0):
{/*this element equals 0, it now needs to be assigned*/} break;
default:
{/*computer finally does something right*/} break;
}maybe nest either one in a do loop till the computer gets its sh*t straight. please tell me why this is wrong, as i am just learning.