3

There's a lot of hype over functional languages right now, and I've spent the last year studying Haskell as my intro to FP as a result. Seeing the advantages FP provides is easy (such as referential transparency and easy concurrency due to the emphasis on immutability).

The question I have been asking myself, now, is whether imperative programming provides any clear advantage over the functional paradigm? I'm not considering more controversial advantages like, oh, more people in the industry know the imperative style. I'm looking more for advantages inherent to the style, regardless of popularity (such as, oh, it's actually better for writing performant low-level code quickly, or it's easier to debug because of x, y, and z, etc). We could pretend that it's an ideal world, if you like, when talking about each paradigm (for example, let's pretend that UNIX was written in SML when talking about functional disadvantages, instead of saying, imperative is advantageous because UNIX is written in C).

It seems I'm having a harder time these days finding out what the advantages of the imperative style are, especially as processor architecture get's more complex so that a Haskell vs C implementation of optimized low-level code looks equally frightening to me.

Josiah
  • 223
  • 3
  • 12
  • 5
    Why do we use screwdrivers to tighten screws and hammers to pound in nails? –  Apr 10 '15 at 16:39
  • 1
    @Snowman: This question is more of, if imperative programming is a hammer, where are the nails? – Josiah Apr 10 '15 at 16:51
  • @gnat: that post appeals to popularity, which is not what I'm asking. What I'm asking about is, given that the claim out right now (that I keep hearing) is that each style has its strengths (implying some objective advantage to each programming style), what are the strengths of the imperative style over the functional? I don't really see many, if any... The argument I keep seeing for imperative is, well, that's what most people know, not here's objectively why it's better. – Josiah Apr 10 '15 at 16:55
  • sounds like you expect answers to [provide a list](http://meta.stackexchange.com/tags/list-questions/info) of each style strengths – gnat Apr 10 '15 at 16:56
  • @gnat: every answer is not equally valid. If claims are to be made that styles have strengths, there must be some objective proof of that. Functional programming is clearly better at forcing/encouraging separation of stateful code from pure code, which makes it better at organizing a program for concurrency. This can be objectively verified using the language constructs. I'm not looking for 'well, I just think it's better because' answers. What is imperative programming actually GOOD at? Is this not an objective question? – Josiah Apr 10 '15 at 17:01
  • 3
    @Snowman because you don't have a nailgun? – enderland Apr 10 '15 at 17:04
  • 4
    Truth be told, you're bumping into the same thing everyone I've seen learn FP has bumped into: There seems to be no reason not to use it, other than people don't know it; and all the negatives people tend to throw around are generally thrown around by folks who haven't taken the time to actually learn FP first. I've yet to find someone who actually knows FP who doesn't prefer it over imperative. – Jimmy Hoffa Apr 10 '15 at 17:21
  • @JimmyHoffa: I entered this question trying not to make that assumption about the imperative approach, which is why I asked it. They type system is actually an advantage for me in Haskell that improves testability and transparency about the mathematical characteristics of the type, while I find it an obstacle that makes testing and maintainability more difficult in Java. – Josiah Apr 10 '15 at 17:29
  • 1
    @Josiah: I program in C++ and Java at work and use Haskell and Lisp in my free time. In my experience, the only advantage of imperative programming is speed, i.e. destructive updates can be faster than updating persistent data structures in FP. Other than that, I cannot think of any other advantages. – Giorgio Apr 22 '17 at 19:38

1 Answers1

6

Well, you already hinted at the answer. Imperative programming is closer to the metal, so it makes more sense in places (like embedded) where you're working closer to the metal. Nobody would bother programming an Arduino in Haskell (well, except maybe for @JimmyHoffa), though programming one in Scheme is not unheard of.

Some other reasons:

  1. Computing model more readily maps to the underlying hardware.

  2. Imperative programs can be written in a functional style, with a small amount of additional effort.

  3. A huge base of preexisting imperative code guarantees the need for people having imperative skills.

The last point is perhaps the most important one. Imperative code has an advantage because its already out there, in great volume. And as long as we keep making languages like Java, we'll be writing more of it.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • So it's still a valid case, then, that C, for example, is still better for embedded despite the complexities of modern processors where your C code caters to the fact that, if you want to optimize, you need to think about keeping things in the cache and avoid hitting the RAM? I agree on point 1, however point 2 seems like opinion and point 3 seems more like a side-note than an actual reason to choose an imperative language. – Josiah Apr 10 '15 at 17:06
  • I think FP advocates would contest the first two, though the first only because of different definitions of simpler (you refer to implementation, FP advocates look at denotational semantics or something similar). –  Apr 10 '15 at 17:07
  • @delnan: Well, not the first. Processors are decidedly imperative. – Robert Harvey Apr 10 '15 at 17:09
  • The keyword they'd take issue with is "simpler", which you wisely edited out ;-) –  Apr 10 '15 at 17:15
  • While the third point sort of touches on something less inherent to imperative programming and more based on language popularity (something I tried to avoid by adding my statement about pretending UNIX was written in ML in the Question statement), I accept that point as completely true and valid. – Josiah Apr 10 '15 at 17:25