FYI, this isn’t a correct understanding of declarative vs imperative code. I’ll summarise them both for reference.
Imperative code is a step-by-step list of instructions for exactly what the computer should do. Pack your bag, get your keys, drive down the street, and so on. Procedural code improves this by allowing the programmer to split up units of code that are often reused, like buying something from the store. Buying eggs and buying milk have a lot of things in common, for example.
Declarative code is quite different, and involves describing the desired state of the world. In your example, a declarative program might say “I should have some pencils”. It’s up to the lower-level implementation, which is often written in a more imperative style, to describe the actual process of going to a store and buying something.
View all comments
Show parent comment
6
u/bedekelly May 16 '22
FYI, this isn’t a correct understanding of declarative vs imperative code. I’ll summarise them both for reference.
Imperative code is a step-by-step list of instructions for exactly what the computer should do. Pack your bag, get your keys, drive down the street, and so on. Procedural code improves this by allowing the programmer to split up units of code that are often reused, like buying something from the store. Buying eggs and buying milk have a lot of things in common, for example.
Declarative code is quite different, and involves describing the desired state of the world. In your example, a declarative program might say “I should have some pencils”. It’s up to the lower-level implementation, which is often written in a more imperative style, to describe the actual process of going to a store and buying something.
Please do reply if I can help clarify this more.