DELAPARRA
Index
Color
NL
Category: Programming paradigms

Generic Programming

Explained by Updated

What is Generic Programming?

Generic Programming: Instead of writing a separate sortNumbers, sortStrings, sortUsers, you write one generic sort that the compiler specialises (or checks) for whatever type T you use it with. Generic programming means writing code once for many types. A generic sort function works for numbers, strings and users alike, instead of having a separate version for each.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Generic Programming

How is Generic Programming used in practice?

The type is a parameter, usually written as T. A Collection<User> and a Collection<Order> share all their code, while the compiler still knows exactly what each one contains.

What should you keep in mind with Generic Programming?

The benefit is reuse without losing type safety. You get autocompletion and errors at compile time, rather than surprises at runtime. TypeScript, Java, C#, Go and Rust support generics natively. In PHP, the same effect comes from docblock annotations checked by PHPStan or Psalm. Laravel's own code uses them extensively, which is why your editor knows what User::query()->first() returns.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with G

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.