c# - Is using LINQ against a single object considered a bad practice? -
i don't mean question subjective.
i google'd time got no specific answers address issue. thing is, think i'm getting addicted linq. used linq query on lists among other things using linq sql, xml, , on. struck me: "what if used query single object?" did. may seem wrong trying kill fly grenade launcher. though agree artistically pleasant see.
i consider readable, don't think there performance issues regarding this, let me show example.
in web application, need retrieve setting configuration file (web.config). should have default value if key not present. also, value need decimal, not string, default return configurationmanager.appsettings["mykey"]
. also, number should not more 10 , should not negative. know write this:
string cfg = configurationmanager.appsettings["mykey"]; decimal bla; if (!decimal.tryparse(cfg,out bla)) { bla = 0; // 0 default value } else { if (bla<0 || bla>10) { bla = 0; } }
which not complicated, not convoluted, , easy read. however, how done:
// initialize compiler doesn't complain when select after decimal awesome = 0; // use enumerable.repeat grab "singleton" ienumerable<string> // feed value got app settings awesome = enumerable.repeat(configurationmanager.appsettings["mykey"], 1) // parseable? grab .where(value => decimal.tryparse(value, out awesome)) // little trick: select own variable since has been assigned tryparse // also, on i'm working ienumerable<decimal> .select(value => awesome) // check other constraints .where(number => number >= 0 && number <= 10) // if previous "where"s weren't matched, ienumerable empty, default value .defaultifempty(0) // return value ienumerable .single();
without comments, looks this:
decimal awesome = 0; awesome = enumerable.repeat(configurationmanager.appsettings["mykey"], 1) .where(value => decimal.tryparse(value, out awesome)) .select(value => awesome) .where(number => number >= 0 && number <= 10) .defaultifempty(0) .single();
i don't know if i'm 1 here, feel second method more "organic" first one. it's not debuggable, because of linq, it's pretty failproof guess. @ least 1 wrote. anyway, if needed debug, add curly braces , return statements inside linq methods , happy it.
i've been doing while now, , feels more natural doing things "line per line, step step". plus, specified default value once. , it's written in line says defaultifempty
it's pretty straightforward.
another plus, don't if notice query larger 1 wrote there. instead, break smaller chunks of linq glory easier understand , debug.
i find easier see variable assignment , automatically think: this had set value, rather @ ifs,elses,switches, , etc, , try figure out if they're part of formula or not.
and prevents developers writing undesired side effects in wrong places, think.
but in end, looks hackish, or arcane.
so come question @ hand:
is using linq against single object considered bad practice?
i yes, it's preference. has disadvantages, leave you. original code can become simpler though.
string cfg = configurationmanager.appsettings["mykey"]; decimal bla; if (!decimal.tryparse(cfg,out bla) || bla < 0 || bla > 10) bla = 0; // 0 default value
this works because of "short circuit" evaluation, meaning program stop checking other conditions once first true condition found.
Comments
Post a Comment