Feeling Recursion
I recently implemented a recursive function in my code and wanted to write about my experience. First, let me say this is not the first time I've written a recursive function and hopefully won't be the last. However, as a developer I jump at the opportunity to write one. As someone who loves to avoid the conventional for each within another for each statement the idea of a recursive function just gets me all excited inside.
Definition:
- A function that calls itself repeatedly, satisfying some condition is called a Recursive Function.
Why would a developer use recursion?
A better question to ask would be why a developer would not use recursion? The classic for loop within a for loop to update the same property for each item in a multi-dimensioned list is just not practical. The code itself is just not clean.
Example of recursion:
I had a scenario where I needed to gather a list of parameters for rows linked via a parent-child relationship. In a nutshell I had a case where a parent row could be linked to several children rows, which also could be linked to several of their own children rows. This could go on and on. I also had to identify the selected row in my list as well for future analysis. Below is a simple code example of recursion.
private void UpdateGridRowInformation(UltraGridRow row, List<GridRowInformation> list)
{
bool isActiveRow = ((Summary)row.ListObject).Key == _activeRowKey;
int parentListIndex = -1;
if (row.HasParent())
parentListIndex = row.ParentRow.ListIndex;
if (row.HasChild(true))
{
//if item has a child then store its Row Information and call recursion //on Child
UpdateGridRowInformation(row.ChildBands[j].Rows
, list);
UpdateGridRowInformationList(parentListIndex, row.ListIndex, row.Band.Key, row.Expanded, isActiveRow, list);
}
else if (isActiveRow)
UpdateGridRowInformationList(parentListIndex, row.ListIndex, row.Band.Key, row.Expanded, true, list);
}
private void UpdateGridRowInformationList(int parentRowIndex, int listIndex, string bandKey, bool expanded, bool active, List<GridRowInformation> list)
{
GridRowInformation gridRowInformation = new GridRowInformation();
gridRowInformation.ParentRowIndex = parentRowIndex;
gridRowInformation.ListIndex = listIndex;
gridRowInformation.BandKey = bandKey;
gridRowInformation.Expanded = expanded;
gridRowInformation.Active = active;
list.Add(gridRowInformation);
}
This code runs through all parent child relationships and adds particular row elements to a list if that row has its own children. This also adds the selected row’s elements.
Conclusion:
Recursion is clean, easy, and extremely beneficial in programming. There are dangers to watch out for though. Just like for loops there must be a way to exit the function cleanly. Keep in mind that because the function calls itself again and again a spiral effect is created. The first time the function is called will not finish until the instance it calls is finished. That instance does not finish until the instance it calls is finished. And this cycle continues. Therefore, if the function continuously calls itself with no calls ever finishing, the application will be stuck running in that part of the code most likely causing the CPU utilization to increase and the application to halt.
In my case, I will eventually get to the last row of the last child, then the function will unwrap and my code will continue. This is a pretty simple example of recursion however it is a very powerful method used for development.
Conclusion Part II:
To conclude from a personal standpoint, I am a happy coder as I have written a recursive function in my application <g>!