What you will learn
This article is going to give a brief overview of what generic programming is. The aim is to impress upon your mind the need to be aware of this concept and make practical use of it as you write code
What is Generic Programming?
Imagine you had to write a function to add 2 numbers. Depending on the language you use, it would look like this, right?
function addStuff(paramOne:number, ParamTwo:number){
let sum = ParamOne+ParamTwo
return sum
}
addNumbers(2, 1)
What if you wanted to add 2 strings "firstname" and "lastname" and return the full name? Well, thanks to the concept of generic programming you only need to write the function once and you can reuse it for that purpose
The way this is achieved is that instead of explicitly specifying that parameters "paramOne" and "ParamTwo" above should be data type: number, you tell the computer that you would define them when you call the function.
If you're coding with TypeScript, It would be something like:
function addStuff<T>(paramOne: T, ParamTwo: T): T { // The "T" variable tells TypeScript that the parameters won't just be numbers
let sum = paramOne+ParamTwo
return sum
}
addStuff(2, 1) // paramOne and ParamTwo are numbers. Output: 3
addStuff('Go', 'Lang') // paramOne and ParamTwo are strings. Output: GoLang
This way, you use one function to add different types of data. Pretty handy, right? I thought so too.
The type of programming languages you can apply this concept with
This concept can be used when writing code with statically typed languages. A language is statically typed if the compiler or programmer expressly declares or infers the data type of variables (such as integers, strings, or floating-point numbers), and the variables' data types cannot be modified or implicitly transformed at runtime. Consequently, the compiler enforces type accuracy throughout the program, and each variable's data type must be specified when it is declared.
JavaScript is not a statically typed language because you can declare a variable like the one below and it would still run
let a; // Is this variable going to take a number? string? JS dosent care.
// The concept of Generic programming can't be applied due to this
// For statically typed languages, this declaration would throw an error because the compiler needs to know what type of data its expecting
Examples of statically typed languages include Java, C, C++, C#, Swift, Go and Rust. In contrast, dynamically typed languages (like Python, JavaScript, and Ruby) determine variable data types at runtime.
Why Generic programming is an important concept
Writing blocks of code with reusable design and consistent, well-defined APIs is a key component of software engineering. The most adaptable capabilities for creating complex software systems will come from components that can operate on both current and future data. This is why generics is quite important.
Conclusion
We have spoken briefly about what generics are and why they are crucial in the field of software engineering. There is a lot more to this concept than I can cover here. Check out the links to some other articles below.
Also, let me know in the comments section what you think. I'd be happy to answer any questions you might have!
Typescript documentation - Generics
Cover Photo by Eric Prouzet on Unsplash