In my humble opinion directory tree example is much more reality alike than some abstract "fibonacci sequence", more complex alternative is searching/traversing binary trees.. Well done sir.
@@troymitchel4790 I've had to use recursion to detect if certain controls were containers and dive into them to format controls or find other controls.
Ahhh recursion, always fun. I still remember the first time I had to use recursion - it broke my brain 😅 Great video! Good example that is easy to follow and explained nicely 👍
Lovely, looking forward to more such videos. A playlist of top interview questions solutions and walkthroughs will be heaven for us if possible of course dear Tim.
@@IAmTimCoreyThanks for your answer, master! I think that I'm missing something in your explanation. If I set a maximum of 4 depth, then it will stop at 0, but if I set -1 meaning infinite, well it will go forever until the end of the depth, no? 🤔
In a perfect world, yes. However, the reason for loops use less than for their check is in case you accidentally skip a number. So in your example, let's say you had a bug in your code where you modified the number twice instead of one time. In that case, it would be 4, 2, 0. Perfect. That works. Ship it to production. Now imagine you get into production, but you are asking for three levels deep instead of four. Now it would be 3, 1, -1, -3, etc. forever. A not equals only checks for one specific exit case. A less than checks for that specific exit case, plus a range of accidental exit cases as well. It isn't perfect, but it is an added protection.
Oh the towers of hanoi from my C÷÷ days comes flodding back !! Might try and revisit that for the fun just to see if it makes more or less sense 20 yrs later !!
That's more the minimum basic about recursions, nothing about endless recursion, stack overflow, etc.. In this sample, the finitness is external given, by the file system.
Nothing about endless recursion or stack overflows? Did you watch the second half of the video? I covered that quite a bit. I didn’t demonstrate creating that type of issue, but I covered preventing it.
@@IAmTimCorey Well, it's essentially necessary, to think about the end of recursion. You presented it as kind of optional to increase comfort. However, I would just encourage you to mark videos as for "bloody beginners" or as "Advanced Topics/News/Upgrades" or so. To find out "Do I already know it" I have no other chance, when to watch the video. If there is 5% new to me, it is worth it. Between "Blazor permanent storage" finding a video "what is a for-loop", or here "what is recursion"; it's kind of mutual exclusive audience.
I often prefer the stack based "recursion", and have it all in the same method namespace RecursionStack; internal class Program { static void Main(string[] args) { Stack stack = new Stack(); stack.Push(@"D:\temp"); string actDir; while (stack.Count > 0) { actDir = stack.Pop(); Console.WriteLine(actDir); foreach (var item in Directory.GetDirectories(actDir)) stack.Push(item); } } }
Me trying to solve this with carry/borrow method: 18 - 9 _____ 8 carries 1 so it becomes 18 So I do 18 - 9 _____ 8 carries 1 so it becomes 18 So I do 18 - 9 _____ 8 carries 1 so it becomes 18 So I do 18 - 9 _____ StackOverflowException
That's what I talked about in the video - make sure you have a floor or a depth limit. In this case, you've made an error in your formula that would create an infinite loop. This would be true if you used a for loop or a while loop as well.
How come you have no code for the program itself at the top? I would expect something like: namespace RecursionDemo { internal class Program { static void Main(string[] args)
In .NET 6, the startup of projects was greatly streamlined. I did a video on what this change is and why it was a good thing: ruclips.net/video/aSNqqZqYTk4/видео.htmlsi=blmTDzn6b1-V50ZZ
Yeah this example is a fundamental and perfect example where recursion is the best place to start. If you start listing files in directories iteratively without a very good reason you are making things way harder and more complex for no reason.
Soften it to: Do it with care, and if possible, avoid it. Like computing the faculty x! of something f(long x) => (x==0) ? 1 : f(x-1)*x; would perfectly work, but a simple for loop is faster and safer. The recursive implementation would even crash on f(40000) and there is no chance to forecast, when the exact crash will happen. (Stack size is limited to 4MB and here we are putting 40.000*8 bytes on the stack.)
My takeaway from this video was: recursion is another tool in your development toolbox, here is how you can use it and, when you use it, here are pitfalls you need to consider. I did not see anything in the video that said recursion should always be used (where there are better options to get the same task done). Even if you think it should not be used, you are not always in control of the code you inherit, so I knowing how a particular feature is used is still useful because you may inherit code which you need to support. I used recursion a lot for tracing precedents and dependents in Excel formulas for forensic auditing, for instance.
Nice video but even with a proper base case you can run into the stackoverflow because C# is a terrible language for recursion. Its better to use Linq or while loops instead because C# lacks tail call optimization. F# does have it though.
It isn’t a terrible language for recursion. You can absolutely use recursion safely in C#. It is more a matter of how the languages are designed and what is the preference. There is a reason why C# doesn’t have TCO. It wouldn’t provide the value that it does to F#. That’s a design choice. blog.objektkultur.de/about-tail-recursion-in-.net/
@@IAmTimCorey Quote from your link: "as a more imperative language, iterative loops are conceptionally fine and (optimizing) deep recursion makes debugging harder as stack traces are destroyed. Thus, C# wants you to write loops instead and the compiler never wants TCO to happen." So as I originally said. Recursion in C# is possible but loops are favoured. The argument about preserving stack traces in C# makes sense but the critique stands C# is terrible for recursion and neither you nor @keyser456 mentioned any way to prevent stack overflows. If its possible that would be great but that information isn't shared. Creating a potential timebomb with recursion in C# is just terrible code. Sure today it works but it could very well lead to stack overflow at some point if for example the folder structure that gets processed increases by a lot. Edit: Even the depth like its implemented in the video does not prevent it. The caller could simply pass a huge number as depth because he wants to do something with all the folders and runs into stack overflow.
In my humble opinion directory tree example is much more reality alike than some abstract "fibonacci sequence", more complex alternative is searching/traversing binary trees.. Well done sir.
Exactly. In the past I had to use this with Active Directory. Also use this with Feature Trees in SolidWorks custom apps.
Thank you!
@@troymitchel4790 I've had to use recursion to detect if certain controls were containers and dive into them to format controls or find other controls.
I would like to have the ability to talk for 20 minutes about the recursion.
Now you have a model. There’s actually a lot more to cover, so you could probably go for 45 minutes if you knew the topic well.
'To understand recursion, you must first understand recursion.'
Joking aside, nice video and +1 for not using a math example!
Thanks!
Cool. Just yesterday I worked around a situation that could otherwise have been handled by this. Thanks Tim!
You are welcome.
Thanks Tim, the example used is more relatable
I’m glad.
Ahhh recursion, always fun. I still remember the first time I had to use recursion - it broke my brain 😅
Great video! Good example that is easy to follow and explained nicely 👍
Thank you!
Lovely, looking forward to more such videos. A playlist of top interview questions solutions and walkthroughs will be heaven for us if possible of course dear Tim.
I'm considering it.
Changing (depth > 0) for (depth != 0) would easily allow you to set a limited or unlimited amount of depth using -1 😊 Good example!
The problem with that is that if you skip over 0, then you will have an infinite depth when you don't expect it.
@@IAmTimCoreyThanks for your answer, master! I think that I'm missing something in your explanation. If I set a maximum of 4 depth, then it will stop at 0, but if I set -1 meaning infinite, well it will go forever until the end of the depth, no? 🤔
In a perfect world, yes. However, the reason for loops use less than for their check is in case you accidentally skip a number. So in your example, let's say you had a bug in your code where you modified the number twice instead of one time. In that case, it would be 4, 2, 0. Perfect. That works. Ship it to production. Now imagine you get into production, but you are asking for three levels deep instead of four. Now it would be 3, 1, -1, -3, etc. forever. A not equals only checks for one specific exit case. A less than checks for that specific exit case, plus a range of accidental exit cases as well. It isn't perfect, but it is an added protection.
@@IAmTimCorey Ok, I understand. Thank you for your precisions!
Great video, thanks...can you please do a video on yield return keyword
Done: ruclips.net/video/AAz8q6dOCYk/видео.htmlsi=2YfMEdeIwp7TFrFw
@@IAmTimCorey thankyou, you are awesome 😎
Amazing Lesson ✅
Thanks!
Great. Thanks 👍
You are welcome.
Oh the towers of hanoi from my C÷÷ days comes flodding back !! Might try and revisit that for the fun just to see if it makes more or less sense 20 yrs later !!
Sounds good.
nicely explained.
Thank you!
In order to understand recursion you have to understand recursion…
That’s why you should understand recursion.
@@IAmTimCorey but to understand recursion…
I've got a video that could help you understand it better: ruclips.net/video/fCoQP7fFqow/видео.htmlsi=9ELP8l-shAzd82sM
Instead of depth could we have used say directories with files only as the base case for the recursion example ?
We did use that when we didn’t specify a depth. When the folder has no subfolders, it stops.
That's more the minimum basic about recursions, nothing about endless recursion, stack overflow, etc.. In this sample, the finitness is external given, by the file system.
Nothing about endless recursion or stack overflows? Did you watch the second half of the video? I covered that quite a bit. I didn’t demonstrate creating that type of issue, but I covered preventing it.
@@IAmTimCorey Well, it's essentially necessary, to think about the end of recursion. You presented it as kind of optional to increase comfort.
However, I would just encourage you to mark videos as for "bloody beginners" or as "Advanced Topics/News/Upgrades" or so.
To find out "Do I already know it" I have no other chance, when to watch the video. If there is 5% new to me, it is worth it.
Between "Blazor permanent storage" finding a video "what is a for-loop", or here "what is recursion"; it's kind of mutual exclusive audience.
something new, will continue the C# start to finish from tomorrow.
Great!
I often prefer the stack based "recursion", and have it all in the same method
namespace RecursionStack;
internal class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
stack.Push(@"D:\temp");
string actDir;
while (stack.Count > 0)
{
actDir = stack.Pop();
Console.WriteLine(actDir);
foreach (var item in Directory.GetDirectories(actDir))
stack.Push(item);
}
}
}
I have to recursively watch this video to understand.
I wouldn't say that, but this video does have a prerequisite: ruclips.net/video/fCoQP7fFqow/видео.htmlsi=rSqQxnh3wqIlwuKE
Me trying to solve this with carry/borrow method:
18
- 9
_____
8 carries 1 so it becomes 18
So I do
18
- 9
_____
8 carries 1 so it becomes 18
So I do
18
- 9
_____
8 carries 1 so it becomes 18
So I do
18
- 9
_____
StackOverflowException
That's what I talked about in the video - make sure you have a floor or a depth limit. In this case, you've made an error in your formula that would create an infinite loop. This would be true if you used a for loop or a while loop as well.
How come you have no code for the program itself at the top? I would expect something like:
namespace RecursionDemo
{
internal class Program
{
static void Main(string[] args)
In newer versions of .net this structure is implied in program.cs.
I think the option is at project setup called Top level statements
Just need to Uncheck "Do not use Top-level Statements" during project setup.
In .NET 6, the startup of projects was greatly streamlined. I did a video on what this change is and why it was a good thing: ruclips.net/video/aSNqqZqYTk4/видео.htmlsi=blmTDzn6b1-V50ZZ
@@IAmTimCorey Ah.. I'm using mainly 4.8 for add-ins to connect with a program specific API. Later is not supported.
First
👍🏻
Please don't do this.
Why? Recursion is an important part of software development.
Yeah this example is a fundamental and perfect example where recursion is the best place to start. If you start listing files in directories iteratively without a very good reason you are making things way harder and more complex for no reason.
Soften it to: Do it with care, and if possible, avoid it.
Like computing the faculty x! of something f(long x) => (x==0) ? 1 : f(x-1)*x; would perfectly work, but a simple for loop is faster and safer.
The recursive implementation would even crash on f(40000) and there is no chance to forecast, when the exact crash will happen. (Stack size is limited to 4MB and here we are putting 40.000*8 bytes on the stack.)
My takeaway from this video was: recursion is another tool in your development toolbox, here is how you can use it and, when you use it, here are pitfalls you need to consider.
I did not see anything in the video that said recursion should always be used (where there are better options to get the same task done).
Even if you think it should not be used, you are not always in control of the code you inherit, so I knowing how a particular feature is used is still useful because you may inherit code which you need to support.
I used recursion a lot for tracing precedents and dependents in Excel formulas for forensic auditing, for instance.
Nice video but even with a proper base case you can run into the stackoverflow because C# is a terrible language for recursion. Its better to use Linq or while loops instead because C# lacks tail call optimization. F# does have it though.
There are plenty of methods you can use in C# to prevent stack overflows and that includes recursion. lol @ F#
It isn’t a terrible language for recursion. You can absolutely use recursion safely in C#. It is more a matter of how the languages are designed and what is the preference. There is a reason why C# doesn’t have TCO. It wouldn’t provide the value that it does to F#. That’s a design choice. blog.objektkultur.de/about-tail-recursion-in-.net/
@@IAmTimCorey Quote from your link: "as a more imperative language, iterative loops are conceptionally fine and (optimizing) deep recursion makes debugging harder as stack traces are destroyed. Thus, C# wants you to write loops instead and the compiler never wants TCO to happen."
So as I originally said. Recursion in C# is possible but loops are favoured. The argument about preserving stack traces in C# makes sense but the critique stands C# is terrible for recursion and neither you nor @keyser456 mentioned any way to prevent stack overflows. If its possible that would be great but that information isn't shared.
Creating a potential timebomb with recursion in C# is just terrible code. Sure today it works but it could very well lead to stack overflow at some point if for example the folder structure that gets processed increases by a lot.
Edit: Even the depth like its implemented in the video does not prevent it. The caller could simply pass a huge number as depth because he wants to do something with all the folders and runs into stack overflow.
Second
👍🏻