|
F-ES Sitecore wrote: That line throws a null exception...can you look at the line that threw the exception and know what the issue is?
Assuming I'm using LINQ, the most likely culprit would be GetAList returning null .
Failing that, I'd have a stack-trace to tell me where the exception occurred.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
But you agree it wouldn't be immediately obvious like it would if you weren't using linq?
|
|
|
|
|
If I wasn't using LINQ, then I'd be able to identify which line in the massive complicated method the exception was thrown from.
Whether it would be obvious why the exception was thrown is a different matter.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
So it's easier to debug without linq?
|
|
|
|
|
No, because you've still got a massive overly-complicated method to dig through to find the cause of the problem.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You instantly know the code that threw the error though so that's a big starting point. Let me give you a better example
string mytext = mydata.Where(a => a.Name != "Admin" && a.ID < 1000).OrderBy(b => b.Surname).SelectMany(c => c.Role).FisrOrDefault(d => d.Updated.Year == DateTime.Now.Year);
We've all seen code like this, right? Let's say it throws a null exception, good luck finding out what is null. If you split your code into functions\loops you don't have that issue.
|
|
|
|
|
Start by changing the code to:
string mytext = mydata
.Where(a => a.Name != "Admin" && a.ID < 1000)
.OrderBy(b => b.Surname)
.SelectMany(c => c.Role)
.FirstOrDefault(d => d.Updated.Year == DateTime.Now.Year);
Your stack trace will include a line number, which will tell you exactly which line you need to look at.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Guess null exceptions aren't a particularly great example of debugging, despite what you might read on CP they're not the hardest issues to track down. When it comes to logic issues with streams of chained linq statements if you want to debug them to find out better why you're getting\not getting the results you want you often have to isolate the steps and loop at them in-turn which is an additional faff you wouldn't have otherwise.
|
|
|
|
|
I still think that's easier to do if you're reusing small methods that do one clearly-defined thing, and which have been thoroughly tested, than if you've lumped all of the implementation into one giant method.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Regardless, debugging is still harder with chains of linq statements, that's the only point I was making.
|
|
|
|
|
Richard Deeming wrote: Writing all of your code in one big Main function is faster than any of this "object-oriented" nonsense.
That is, almost certainly, not true. With one big function, the optimizer will have to practically shut-down. Many smaller functions can be highly optimized.
Richard Deeming wrote: And using C or assembly will be much faster than this JIT-compiled C# nonsense.
Again, real world examples have shown that letting the computer do things like managing your resources, is much faster than trying to do it yourself manually.
Truth,
James
|
|
|
|
|
I would be a bit surprised if your first point was true. Please give me a couple examples of optimizations that depend on function size.
|
|
|
|
|
Optimization largely depends on tracking the lifetime of variables:
for(int i =0; i< 100; ++i)
{...}
will be better optimized, than this...
int i;
for(i =0; i< 100; ++i)
{...}
just because the compiler knows that "i" is never used again outside that for loop. In the latter, space must be allocated for i on the stack, and it must be stored there. In the first, "i" may live at it's entire existence in a register.
Now, in an example as small as the above, a good compiler may still realize that even the second "i" is not used again, but the larger that function gets, with more things to track, the optimizer begin to give up.
Truth,
James
|
|
|
|
|
That might be true but it comes at the cost of jumps and stack management for function calls. I think you will be hard-pressed to find an example of one block of code that runs slower than similar code split into more functions.
|
|
|
|
|
But then, what about library functions? Are you going to inline every call to ToUpper() or Trim()?
If you do, you have a unmanageable mess.
If you don't, then you're back to the costs of jumps and stack management, so what's a few more?
Truth,
James
|
|
|
|
|
"What's a few more" is often significant overhead.
I don't know what argument you think I'm making. The comment that is the topic of my comments simply said that writing your code in a big main function is faster that all this object oriented stuff but probably a bad idea. I'm agreeing with that.
|
|
|
|
|
Mike Marynowski wrote: I would be a bit surprised if your first point was true. Please give me a couple examples of optimizations that depend on function size.
Small functions can be in-lined by the optimiser, so this code
foreach(string x in y)
{
x = x.Trim().ToLower();
DoSomething (x);
}
function DoSomething(string s)
{
if (s.StartsWith("hello"))
{
s = "test";
}
}
might be optimised to this
foreach(string x in y)
{
x = x.Trim().ToLower();
if (x.StartsWith("hello"))
{
x = "test";
}
}
thus avoiding a code jump\stack update etc.
|
|
|
|
|
Yes, but that optimization doesn't have to be made if you are putting everything into a big block of code lol. I'm not advocating for that approach, just saying that splitting into functions is unlikely to boost performance...in the best case it will match performance after optimizations, which is basically what is happening in this example.
|
|
|
|
|
I think I need to buy one of those "sarcasm" flags.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well I do find it a lot faster to write. I would expect it to have poorer performance, but don't know how bad. I believe is that if you have performance issues, find out the methods that use the most resources, and optimize them. This is where get most bang for the buck.
|
|
|
|
|
I use it - though not the .ForEach on the end - because there are times when it provides a reliable, succinct, and clear way to process list (or other collection) based data.
var vidsWithOutPics = vidList.Except(vidsWithPics).Where(v => !v.IsAlternateTitle);
Or
var inDuration = DiskFile.GetAll().Where(df => !df.HasDuration).Select(df => df.Video).Distinct();
Or
var noSizeList = videoFiles.Where(file => file.Bytes < 0 && files.Contains(file.Location));
All I'm doing is "hiding" the loop so I don't have to write it!
Yes, I could write each of those as loops - they aren't at all complex - but they would be longer; they would need debugging each time I wrote them.
The other alternative would be to use Linq syntax, and that's pretty horrible!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
But surely you need to debug this as well? Just in case? And that would be harder than if it were a normal loop
|
|
|
|
|
Yes - but you need to debug the criteria it's using rather than the complete code - which isn't complicated. Would you like to write and debug "Except" each time you need it?
No - so you'd write it once and call it from multiple places. Which is exactly what I do when I use aCollection.Except(anotherCollection) - except I don't have to write it in the first place!
And you have to admit that
var inDuration = DiskFile.GetAll().Where(df => !df.HasDuration).Select(df => df.Video).Distinct(); Is a lot more readable than the "home brew" version using methods:
var inDuration = Distinct(Select(Where(DiskFile.GetAll(), Video), HasDuration));
Where it's a PITA to just make sure the brackets match up!
Yes, Linq methods can be slower to execute - but sometimes the absolute speed isn't that important, but reliability and ease of maintenance is.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
OriginalGriff wrote: Would you like to write and debug "Except" each time you need it? Actually yes, so I wouldn't end up in that situation of horribly nested function calls in your second code block.
|
|
|
|
|
Instead having loads of repeated code everywhere for no real benefit? are you trolling?
|
|
|
|