DELAPARRA
Index
Color
NL
Category: Programming paradigms

Declarative vs. Imperative

Explained by Updated

What is Declarative vs. Imperative?

Declarative vs. Imperative: These are two opposite styles of writing code. Imperative code describes every step of the algorithm. Declarative code describes the desired outcome and lets the runtime figure out the steps. Imperative and declarative are two ways of writing code. Imperative code gives step-by-step instructions: loop over the users, check each one, add the active ones to a new list. Declarative code states the outcome: give me the active users.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Declarative vs. Imperative

How is Declarative vs. Imperative used in practice?

Most real code mixes both. A Laravel collection call like $users->filter(fn ($u) => $u->active) is declarative at the top, with a small imperative piece inside.

What should you keep in mind with Declarative vs. Imperative?

Neither style is better in general. Declarative code is usually easier to read and harder to get wrong for common tasks such as querying, filtering and describing infrastructure. Imperative code gives full control when you need a specific algorithm or fine-tuned performance. A good rule is to state what you want as declaratively as the tools allow, and drop down to imperative steps only where it adds clarity or speed.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with D

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.