Origins of The Generics

The Generics, which is included in many programming languages, actually has a mentality like Object-Oriented and Functional Programming. It is necessary to look up the origins based on mathematics to explain what Generics actually refers.

Fırat Payalan
ITNEXT

--

Photo by Sigmund on Unsplash

Generics is a programming type initially included in the Java programming language in 2004. It uses “Types” to signify objects on compile-time safety. Oracle implies three main motivations of Generics usage.

Stronger types at compile time 👊

Type safety is an attribute guarantee that the data in the specific instruction will properly be defined and behaved under any conditions. Once define a string type, the programming language does not alter the behaviors of a string-typed variable. However, type unsafe programming languages such as Javascript, PHP have no additional control while manipulating the variable.

Basically, two kinds of type checking happen. compilers do an analysis of a source code on a compilation phase. some of them make verification for variable assignation or function execution with their types. this kind of process happens in static type checking enabled compilers. static typed checked programs have a fast execution process. because no additional type checking has been made at runtime but they do not ensure the program will fully type safe.

Static type checking is insufficient under some conditions. Let’s think of a case. TypeScript is the language based on static type checking. The typescript compiler converts the code into a Javascript file. Javascript has no type checking feature. At the end of the day, javascript will allow any kind of input into any kind of variable even though we write a typescript code. We need control at runtime.

Dynamic type checking enabled languages to smoothly evaluate instructions. The program will throw an interrupt when an invalid behavior could occur. Dynamically typed programs a little bit slower but fully ensure the type safety perspective.

Reducing casting operation 👇

Uncle bob refers that a solid and clean code Must be avoided with some sort of encodings. Each casting operation is an encoding process. It distracts developers from the main motivation of code blocks. code readers keep focusing on the actual business, not the utility things.

Without generics, we need additional casting operators to make type-safety at compilation especially at Collection classes.

However, the casting operator does not fully ensure type safety at compilation time. Some inheritance situations may beat the compilation but JVM catches on runtime with ClassCastException(Dynamic type checking).

Java mentioned the types as the “Erasure” keyword to keep strong type-safety on the compilation phase regardless of any runtime issues.

Empower developers to make generic programming things ⚡️

All possibilities should be revealed by making lots of analyses in the algorithm development. An algorithm has a scope that mainly points to the solution surface. Many analyses might be attempted to clarify the scope. However, a good algorithm can be expressed with minimal assumptions. The generic programming paradigm motivates making lesser postulations to define the algorithm.

Sometimes we just start a concrete implementation of an algorithm. As time goes by, algorithm scope will not be changed yet the assumptions are getting advanced. It is the second main motivation for Generic programming to get as generalized as possible from our algorithm without losing efficiency.

Generic programming is fundamental to algorithm implementation without considering types or any quantifiers. Makes algorithms universally performed and also keeps syntax pure with no duplication.

The Reality of “Generics”

Mathematical components step into the stage. There is a form called “Quantifier” to represent types in Mathematical Logic. Many definitions have been made of quantifiers through time. William Hamilton has achieved its current definition.

There are two main types of quantifiers that exist. Universal quantifier denotes that all the members of the universe(a.k.a domain of discourse). We use the “for every x” statement to explain this quantifier. For example; The logical statement of “For every number in x, x² will be equal or greater than 2*x” exactly satisfies all of the numbers in the universe.

The logical assumption of “For every numbers in x, x² will be equal or greater than 2x”

On the other hand, there are some quantifiers that not universally acceptable. Existential quantifiers assure that at least a singular satisfaction is maintained. It is not a contrast of the universal quantifiers, only hits a single assertion. The keyword “for some” is used to explain this quantifier. For example; the Statement “For some numbers in x, x³ is equal or lesser than x” explains that not every x number verifies the prediction of x³ < x, only negative numbers correct the statement and of course negative number is some values in the number universe.

The logical assumption of “For some numbers in x, x³ is equal or lesser than x”

It is actually a difficult issue to express universal quantifiers. Because we are talking about all the values in the universe that correspond to this quantifier. At this point, the concept of variable emerges to tell our quantifier with a correct expression. Considering the universal quantifiers, I will now include a certain variable in the evaluation of a quantifier. As an example, I made the expression “apples are sweet” with the universal quantifier. But I have doubts about the accuracy of this statement. Here I am going to arrange “Red apples are sweet” to make a correct statement. Now my expression is “bounded quantifier” instead of a universal quantifier.

Conclusion

At this point, if we talk about Generics, I basically define a type as T. This actually corresponds to the universal or existential quantifier in logic.

This is the equivalent of giving values such as T, U, C, A, B, D in programming languages. Here I need a “bounded” value to implement my statement in code.

I tried to explain in this article that Generics comes from completely mathematical origins. While writing this article, I was always questioning that functional programming does indeed have an advantage over the OOP paradigm. At this point, when I learn that the Generics structure used in almost all OOP languages actually comes from a mathematical expression, I think that the origin of the OOP paradigm is actually down to functions and the mathematical expressions that these functions depend on. In my next articles, I will try to write something on different programming language concepts from this point of view.

--

--