|
Marc Clifton wrote: The ToList() seems superfluous
It (and ToArray) nearly always is. (But I'm not about to try to read that code.
|
|
|
|
|
You're my hero. I also find myself writing lines like this. I kinda consider them a guilty pleasure, they may not look nice, but I like them. Started all the way back at the end of my school time, I was in the Maple math class and my teacher always told me to use variables, they're aplenty and free. He kept telling me every single time.
|
|
|
|
|
In theory, you can write your entire application on a single line.
You're writing for people though, not for machines.
So yeah, make two separate lines that make it more readable and the compiler will inline them giving you no added memory or performance impacts.
I'd probably also do this:
OutputImage(file, allFaces.Where(f => f.Proportion > lowerBound && f.Proportion < upperBound)
.ToList()
.OrderBy(f => f.Proportion)
.ThenByDescending(f => f.Rectangle.Height)
.FirstOrDefault());
|
|
|
|
|
Those variables are used in only one place, but they are accessed more than once, since they live in a Where lambda. So it makes good sense to extract them!
Or can you assure that the Where lambda is called at most once? If yes, then drop it, because in that case, LinQ is overkill.
Rolf
|
|
|
|
|
PeejayAdams wrote: double lowerBound = double.Parse(ConfigurationManager.AppSettings["LowerBound"].ToString());
double upperBound = double.Parse(ConfigurationManager.AppSettings["UpperBound"].ToString());
AppSettings is a NameValueCollection . The indexer returns a string. What are those extra .ToString() calls doing in there?
PeejayAdams wrote: .ToList().OrderBy(f => f.Proportion).ThenByDescending(f => f.Rectangle.Height).FirstOrDefault()
Why are you reading the whole thing into a List when you only want one result? You can almost certainly drop the .ToList() call.
Also, are you sure the OutputImage method won't barf if you pass in null as the second parameter?
double lowerBound = double.Parse(ConfigurationManager.AppSettings["LowerBound"]);
double upperBound = double.Parse(ConfigurationManager.AppSettings["UpperBound"]);
var dirkBenedict = allFaces
.Where(f => f.Proportion > lowerBound && f.Proportion < upperBound)
.OrderBy(f => f.Proportion)
.ThenBy(f => f.Rectangle.Width)
.FirstOrDefault();
OutputImage(file, dirkBenedict);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It won't barf, but yes, those ToString()s are redundant. This is some code ripped from a very Q&D POC project that just seemed to be a good example of the more general issue of how to format long lines.
Love the dirkBenedict variable name - my new mission is to find a way to get dwightSchultz in there!
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
OutputImage(file,
allFaces.Where(f =>
f.Proportion > double.Parse(ConfigurationManager.AppSettings["LowerBound"].ToString()) &&
f.Proportion < double.Parse(ConfigurationManager.AppSettings["UpperBound"].ToString()))
.ToList()
.OrderBy(f => f.Proportion)
.ThenByDescending(f => f.Rectangle.Height)
.FirstOrDefault()); This is probably how I'd break this out without introducing temporary values. That said, to me the whole statement would be very difficult to debug. You've got several things going on where it would be useful to see the intermediate values.
Also, the .FirstOrDefault() at the end tells me that, since you want only one value from the set, simple iteration over the AllFaces collection would probably be less expensive than the sort (.OrderBy(...) ).
Software Zen: delete this;
|
|
|
|
|
class ConfigWrapper
{
public static double LowerBound { get { return double.Parse(ConfigurationManager.AppSettings["LowerBound"].ToString()); } }
public static double UpperBound { get { return double.Parse(ConfigurationManager.AppSettings["UpperBound"].ToString()); } }
...
}
...
var face = allFaces.Where(f => f.Proportion > lowerBound && f.Proportion < upperBound).ToList()
.OrderBy(f => f.Proportion).ThenByDescending(f => f.Rectangle.Height).FirstOrDefault();
OutputImage(file, face);
Wrap the config so that you can be sure it's always accessed correctly, and can potentially do lazy loading and data validation. This also makes changing hard coded app.config settings into user configurable ones later on a lot less painful.
Unless I was doing something that LINQ couldn't translate to SQL or that caused it to do something terribly inefficient, I'd drop the `ToList()` in the middle of the method chain as useless overhead.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
Yes, you should do the latter, even though lowerBound and upperBound are only used once. The reason is maintainability. This shouldn't even be a question for anyone who has ever had to read and modify someone else's code.
|
|
|
|
|
I don't think it's necessary to add the extra variables to make the code readable. Just split the lines where it makes sense. That should be enough.
OutputImage(file, allFaces.Where(f =>
f.Proportion > double.Parse(ConfigurationManager.AppSettings["LowerBound"].ToString()) &&
f.Proportion < double.Parse(ConfigurationManager.AppSettings["UpperBound"].ToString())).ToList()
.OrderBy(f => f.Proportion)
.ThenByDescending(f => f.Rectangle.Height)
.FirstOrDefault());
|
|
|
|
|
I haven't read all the responses yet, by here's my take in general terms.
As devs, we sometimes encounter situations where competing priorities become evident.
Like when the ideal of simplicity, readability, and maintainability run counter to runtime efficiency.
I always bias my choices with empathy for the shmuck (fixer-person or enhancer-person) that comes later and must figure out what the I was trying to do.
Impossible-to-easily-parse code is a follow-on bug waiting to happen.
There is nothing more insidiously degrading to a codebase than cascading bugs due to ill-informed, shoot-from-the-hip changes because the code is too difficult to grok. And we all know it happens.
The kicker here is, unless it's message-loop-style code that runs continuously for the life of execution, it won't ever be an issue (AND, the compiler may even ultimately produce the same code.)
endRant
Cheers,
Mike Fidler
"I intend to live forever - so far, so good." Steven Wright
"I almost had a psychic girlfriend but she left me before we met." Also Steven Wright
"I'm addicted to placebos. I could quit, but it wouldn't matter." Steven Wright yet again.
|
|
|
|
|
Over the years, I've developed an attitude about this.
If I want others (including myself in a later incarnation) to stay away, PLEASE do not think this is easy by being ""readable"", I densify, (un)justly believing in more efficient code (from a compiler or machine viewpoint).
Intermediate stuff should be lighter, though.
|
|
|
|
|
Is comatose when your foot falls asleep?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I thort it's what hip hop hobbits do for 1np1rat1on
Message Signature
(Click to edit ->)
|
|
|
|
|
Yes, and walking on pins and needles is the (pedi)cure.
|
|
|
|
|
or something Italians with hairy feet do?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
|
Great. Lately I've been dealing with a bit of restless leg syndrome (it's a real thing), and now you've just reminded me of it.
|
|
|
|
|
Easier to handle than a comanose
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
Hi All,
Just trying to see if the network is back. Issues my end!
|
|
|
|
|
I don't know - can I get back to you?
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
Barely. Thanks for asking.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
phil.o wrote: Thanks for asking.
He wasn't talking to you silly.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Lights are on but nobody's home!
They call me different but the truth is they're all the same!
JaxCoder.com
|
|
|
|
|
Considering I left the house this morning without power, I suspect this to be the case right now since my neighbor kindly informed me that the power was back on. Good news is the fridge/freezer was already empty in advance of my vacation later this week.
|
|
|
|