What’s Generic Programming?

Print anything with Printful



Generic programming creates efficient code that can be applied to multiple situations without changes. It debuted in Ada and CLU, and is used in Java and C++. It allows for the use of one piece of code in different programs. It is used in statically typed languages and can handle different types of user input.

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

In the 1970s, generic programming made its debut in the Ada and CLU programming languages. Soon after, other programming languages ​​like Java and C++ started using generic programming to simplify programming code by 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,” “patterns,” and “parameterized types” have all been used at one time or another to refer to instances of generic programming.

To understand this type of programming, it is important to understand the basics of how a programming language works. If, for example, Paul wants to write a program that adds two numbers, he will type computer code to add two objects. He would then tell the computer that those two objects are numbers and that the final answer should also be a number.

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

Not all programming languages ​​need the concept of general purpose programming 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 changed while the program is running. For this reason, if a programmer specified that user input would be in the form of letters and the user typed a number, the program could not use the input. So programmers try to predict all kinds of logical data that a user might enter, be it numbers, letters, or symbols, and create a program that can adjust accordingly.




Protect your devices with Threat Protection by NordVPN


Skip to content