Why do neurons produce those all-or-none electrical events named action potentials?
One theory, based on the coding paradigm, is that the production of action potentials is like analog-to-digital conversion, which is necessary if a cell wants to communicate to a distant cell. It would not be necessary if neurons were only communicating with their neighbors. For example, in the retina, most neurons do not spike but interact through graded potentials, and only retinal ganglion cells produce spikes, which travel over long distances (note that there is actually some evidence of spikes in bipolar cells). In converting graded signals into discrete events, some information is lost, but that is the price to pay in order to transmit any signal at all over a long distance. There is some theoretical work on this trade-off by Manwani and Koch (1999).
Incidentally, this theory is sometimes (wrongly) used to argue that spike timing does not matter because spikes are only used as a proxy for an analog signal, which is reflected by the firing rate. This theory is probably not correct, or at least incomplete.
First, neurons start spiking before they make any synaptic contact, and that activity is important for normal development (Pineda and Ribera, 2009). Apparently, normal morphology and mature properties of ionic channels depend on the production of spikes. In many neuron types, those early spikes are long calcium spikes.
A more convincing argument to me is the fact that a number of unicellular organisms produce spikes. For example, in paramecium, calcium spikes are triggered in response to various sensory stimuli and trigger an avoidance reaction, where the cell swims backward (reverting the beating direction of cilia). An interesting point here is that those sensory stimuli produce graded depolarizations in the cell, so from a pure coding perspective, the conversion of that signal to an all-or-none spike in the same cell seems very weird, since it reduces information about the stimuli. Clearly, coding is the wrong perspective here (as I have tried to argue in my recent review on the spike vs. rate debate). The spike should not be seen as a code for the stimulus, but rather as a decision or action, in this case to reverse the beating direction. This argues for another theory, that action potentials mediate decisions, which are by definition all-or-none.
Action potentials are also found in plants. For example, mimosa pudica produces spikes in response to various stimuli, for example if it is touched, and those spikes mediate an avoidance reaction where the leaves fold. Those are long spikes, mostly mediated by chloride (which is outward instead of inward). Again the spike mediates a timed action. It also propagates along the plant. Here spike propagation allows organism-wide coordination of responses.
It is also interesting to take an evolutionary perspective. I have read two related propositions that I found quite interesting (and neither is about coding). Andrew Goldsworthy proposed that spikes started as an aid to repair a damaged membrane. There is a lot of calcium in the extracellular space, and so when the membrane is ruptured, calcium ions rush into the cell, and they are toxic. Goldsworthy argues that the flow of ions can be reduced by depolarizing the cell, while repair takes place. We can immediately make two objections: 1) if depolarization is mediated by calcium then this obviously has little interest; 2) to stop calcium ions from flowing in, one needs to raise the potential to the reversal potential of calcium, which is very high (above 100 mV). I can think of two possible solutions. One is to trigger a sodium spike, but it doesn't really solve problem #2. Another might be to consider evenly distributed calcium channels on the membrane, perhaps together with calcium buffers/stores near them. When the membrane is ruptured, lots of calcium ions enter through the hole, and the concentration increases locally by a large amount, which probably immediately starts damaging the cell and invading it. But if the depolarization quickly triggers the opening of calcium channels all over the membrane, then the membrane potential would increase quickly with relatively small changes in concentration, distributed over the membrane. The electrical field then reduces the ion flow through the hole. It's an idea, but I'm not sure the mechanism would be so efficient in protecting the cell.
Another related idea was proposed in a recent review. When the cell is ruptured, cellular events are triggered to repair the membrane. Brunet and Arendt propose that calcium channels sensitive to stretch have evolved to anticipate damage: when the membrane is stretched, calcium enters through the channels to trigger the repair mechanisms before the damage actually happens. In this theory, it is the high toxicity of calcium that makes it a universal cellular signal. The theory doesn't directly explain why the response should be all-or-none, however. An important aspect, maybe, is cell-wide coordination: the opening of local channels must trigger a strong enough depolarization so as to make other calcium channels open all over the membrane of the cell (or at least around the stretched point). If the stretch is very local, then this requires an active amplification of the signal, which at a distance is only electrical. In other words, fast coordination at the cell-wide level requires a positive electrical feedback, aka an action potential. Channels must also close (inactivate) once the cellular response has taken place, since calcium ions are toxic.
Why would there be sodium channels? It's actually obvious: sodium ions are not as toxic as calcium and therefore it is advantageous to use sodium rather than calcium. However, this is not an entirely convincing response since in the end, calcium is in the intracellular signal. But a possible theory is the following: sodium channels appear whenever amplification is necessary but no cellular response is required at that cellular location. In other words, sodium channels are useful for quickly propagating signals across the cell. It is interesting to note that developing neurons generally produce calcium spikes, which are then converted to sodium spikes when the neurons start to grow axons and make synaptic contacts.
These ideas lead us to the following view: the primary function of action potentials is cell-wide coordination of timed cellular decisions, which is more general than fast intercellular communication.