We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What is Generic Programming?

By Jessica Reed
Updated: May 16, 2024

Generic programming is one popular type of computer programming written in such a way that it creates the most efficient code possible while allowing the code to apply to as many situations as possible without requiring any changes to the original code itself. Once the code is written, it can only perform the exact functions it was written for. By using generic programming to create codes that work in a number of different situations, while still performing the same basic, overall function, programmers can use a single piece of code in different programs without ever making changes to the original.

During the 1970s, generic programming made its debut in the Ada and CLU programming languages. Soon after, other programming languages such as Java and C++ began to use generic programming to simplify programming code while allowing the same code to be used in multiple scenarios. Each programming language has a particular way of using this code and different terms to describe it. "Generics," "templates," and "parameterized types" have all been used at some point or another to refer to instances of generic programming.

To understand this type of programming, it's important to know the basic concepts of how a programming language works. If, for example, Paul wants to write a program that adds two numbers together, he would type out the computer code to add two objects. He would then tell the computer that those two objects are numbers and that the final answer should be a number as well.

While the program will work as long as Paul is adding two numbers, it will crash if he tries to add anything else together. If Paul decided to string sentences together to form a paragraph, the program would crash because it would find letters and not numbers. Paul could fix this problem, however, by using generic programming to tell the original program to accept a number of variables — both numbers and letters — and thus the program could create sentences or perform addition.

Not all programming languages need the concept of generic programming for them to be efficient. Those that use it are statically typed languages. This simply means that the code is set in stone, so to speak, and cannot be altered while the program is running. For this reason, if a programmer specified that input from the user would be in the form of letters and the user typed a number, the program could not use the input. Thus programmers try to foresee all logical data types a user could input, be it numbers, letters, or symbols, and create a program that can adjust accordingly.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.