2

Just read an article teaching me about the difference between imperative and declarative programming (I wasn't aware of the names of these two paradigms before) and it really made my blood boil.

Someone please correct me if I am wrong:

Imperative programming:

// Code that I wrote:
int add (int x, int y) {
return x + y;
}
//Code that I also wrote:
int main () {
int a = add(1, 2);

return;
}

Declarative programming:

// Code that I did not write.
int add (int x, int y) {
return x + y;
}
//Code that I did write:
int main () {
int a = add(1, 2);

return;
}

If this is literally the only difference in thinking that I'm pissed this is even a discussion, because declarative programming would just be a layer on top of imperative.

Comments
  • 11
    either i'm still drunk from last night cause i can't see difference or there's actually no difference between your two snippets..
  • 4
    As everything ends up in machine instructions, then everything is a layer on top of imperative programming.

    In declarative programming we specify *what* we want and leave the *how* to the compiler. Less code for us to write and less having to think like a computer.
  • 1
    @myss Read comments.
  • 9
    It's a bad example as it does not show any difference between the two.
  • 3
    @haze thank you! Finally can get my freaking penguin 🐧😎
  • 1
    @myss Congrats! I didn't notice your reputation when I ++'s you. And now I broke the number. :P
  • 2
    Imperative: Take a bowl, mix the ingredients to make batter, put into an oven and you have a cake

    Declarative: You get a cake by baking batter which you get by mixing it from ingredients
  • 1
    @myss check the comments
  • 0
    @platypus that really isn't a different paradigm though, is it? I mean all your doing is using imperative programming that someone else wrote. All that is is using libraries. I don't understand why that would qualify as an entirely different paradigm, because then even C would be considered a declarative language if you use enough libraries
  • 0
    @12bitfloat yeah but you need to understand what ingredients and how to mix it properly. And if you don't Inherently know then you look for instructions.... Still imperative
  • 0
    @platypus also a fundamental problem with that thinking is unknown behavior. Not knowing how is a fairly huge problem when dealing with actually important things like security or performance, isn't it?
  • 2
    @deviestRanter You can have different paradigms at different levels of abstract. But, ultimately, if running code on a standard computer, you are running imperative code.

    However, you can *write* declarative code at a higher level of abstraction and let the tooling/libraries do the heavy lifting.

    As for trusting the libraries. Of course, there an issue there. But I have to trust all sorts of third party code in my stack. Unless you are the NSA, GCHQ, etc, the economics of personally validating your stack aren't viable.
Your Job Suck?
Get a Better Job
Add Comment