There is a contrast in the way I study these two topics.
For any computer science student, the topics of recursion and design patterns are of paramount importance. Recursive programming is normally considered tricky while applying concepts of design patterns in a particular problem is also not that straightforward. In this article, I shall provide the approach I take while studying these two topics so that later I can develop my own recursive program as well as apply design patterns concepts for a problem.
Recursion in programming
Recursion is a computer programming technique where a function calls itself. In recursive function, the programmer needs to be careful to provide an exit condition, or else it will go into an infinite loop. Recursion makes use of stack and it is normally difficult to follow the flow of control.
During college days, I had difficulty understanding code written using recursion and it was almost impossible for me to solve a problem by applying recursion on my own. I faced the problem because I tried to follow the flow of control.
Example: The first recursive program that a programmer normally encounters is to find the nth fibonacci number. If fib(int n) is the function then the recursive part contains fib(n — 1) + fib(n — 2). If I try to understand the recursive call by following the flow of control then I may get lost.
My approach: I do not give emphasis to the way flow of control happens in a recursive function. Rather I just think about the exit condition and as far as the recursive call is concerned, I just assume it will do its part and give me the result.
In the above example of fibonacci series, I first write the exit condition — if (n <= 1) return n; and as far as fib(n -1) + fib(n-2) is concerned, I assume fib(n -1) will give (n-1)th fibonacci number and fib(n-2) will give (n-2)th fibonacci number. Through using this approach I do not totally discard the way stack is used. If I have to write some statements after making the recursive call then understanding the way stack is used is also important. But one should not get lost in the flow of control or else it will be difficult to write a function with recursion.
Design patterns are typical solutions to commonly occurring problems in software design.
One may not appreciate design patterns until one gains some experience. The common tendency of a college student is to get the work done but as one gains experience, the quality and maintainability of the software become important. Then one realizes the importance of design patterns.
When I started studying design patterns by going through some videos online, it seemed to me that I was able to follow the tutorial. At any point in the tutorial, the complexity of understanding what the code is doing was not as complex as a recursive program. One may go through the whole design pattern tutorial and understand what it does but when it comes to application, one may find difficulty.
My approach: While going through a design pattern tutorial, I try to understand the motivation behind the way classes are designed. I try to find the answer as to why the author thought of a particular approach. I also deeply follow the flow of control contrary to the recursive program where I do not provide emphasis to it.
Following the above approach, I just do not understand how the problem was solved via the design pattern but also get into a position to effectively apply the concepts when faced with similar problems.
In most of the interviews in the computer programming field, there invariably are questions related to recursion and design patterns. Even otherwise, while in a day to day job also, these concepts are heavily used. There is a difference between how I study recursion vs. design patterns. For recursion, I give less emphasis to flow of control while in design patterns I give a lot of weightage to motivation and flow of control. At least for me, keeping this strategy helped and now I am independently able to write recursive functions and am getting better at design as well.
Powered by WPeMatico