Categories
computer-science design-patterns programming recursion technology

Studying Recursion vs. Design Patterns

There is a contrast in the way I study these two topics.

Photo by Chris Ried on Unsplash

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 pattern

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.

Conclusion

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.


Studying Recursion vs. Design Patterns was originally published in ILLUMINATION on Medium, where people are continuing the conversation by highlighting and responding to this story.

Powered by WPeMatico

Categories
blockchain crypto cryptocurrency tech technology

BSN Launches, First Six Chains Pump

China-backed BSN launched its portal last Saturday; the relevant blockchains have seen significant price increases.

Powered by WPeMatico

Categories
blockchain cryptocurrency finance tech technology

Yield Farming: How To Earn Passive Income With Appreciating Crypto Tokens

A basic primer on what “yield farming” is

Powered by WPeMatico

Categories
artificial-intelligence business future science technology

Why High-Tech Companies Want to Acquire Each Other Nowadays?

Acquisitions may seem simple at first glance like a usual business activity. But there is always a bigger reason behind them.

Powered by WPeMatico

Categories
education philosophy science tech technology

Autodidacticism: Teach Yourself To Succeed

How to roll with the punches as everything changes

Powered by WPeMatico

Categories
data information internet privacy technology

Is Your Privacy is Safe?

Powered by WPeMatico

Categories
communication economy india internet technology

India’s First-Ever Submarine OFC Cable To Boost 4G Connectivity in Andaman and Nicobar Island

All you need to know about Submarine OFC Cable

Powered by WPeMatico

Categories
instagram internet social-media technology tik-tok

The Alternative to NoSurf in 2020

For those who have used social media long enough can relate to the dreaded late-night refresh with nothing new showing up. Fortunately for…

Powered by WPeMatico

Categories
art creativity photography photoshop technology

How to Colorize an Old Photo

Bringing life to an old photo is possible with the right tools.

Powered by WPeMatico

Categories
future psychology science-fiction social-media technology

Are Aliens Introverted Or Extraverted?

If they come here to uplift this planet with their presence — how social will they be?

Powered by WPeMatico