Generics & Open recursion

Flare
2 min readMar 10, 2022

1. Generics

If you are a fan of reusing code then generic programming is your best friend! Generics offer the possibility for primitive data types and user-defined types to be passed as an argument for functions (geeksforgeeks, 2019)

By taking generic programming into consideration, you should be able to solve problems that require applying the same algorithm with all data types, for the reason that the main purpose of using Generics is to avoid repeating the same algorithm when the only variable is the data type.

Taking Java programming language, for example; We can define a Generic by creating a class that takes the data type as a parameter, and then in the phase of creating a new instance, we pass the data type we’d like to use as an argument.

The following code describes the use case above:

2. Open recursion

Open recursion can be explained as a property that comes with all sorts of programming languages that support objects and classes and it is either referred to by this or self (Nystrom, 2013)

Open recursion is not as same as recursion, because recursion is a method calling itself while open recursion is when a method is able to invoke another method that belongs to the same object.

Below is an example of open recursion in Python:

With the code above we’ll get an output of:

  • I am super class
  • I am child class

So the summary is that we created a Parent (superclass) that has two methods: name() and className(), then we created a Child (child class) that overrides className(). When creating a new instance to see the output of the code we wrote, we only call name() but still, we get the desired output for both classes.

That's what we call open recursion for the reason that name() calls className() and they are both bound to the same object in the parent class; but for the child class, we get a different output because className() was overridden, so the object of the child class is a frame of reference for a different version of the className method.

References:

- geeksforgeeks. (2019). Generics in C++. Retrieved from https://www.geeksforgeeks.org/generics-in-c/

- Nystrom, B. (2013). What is “Open Recursion”?. Retrieved from https://journal.stuffwithstuff.com/2013/08/26/what-is-open-recursion/

--

--

Flare

I write what I needed to read in the past | Opinions are my own.