Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When imperative style fits better?

From the Programming in Scala (second edition), bottom of the p.98:

A balanced attitude for Scala programmers

Prefer vals, immutable objects, and methods without side effects. Reach for them first. Use vars, mutable objects, and methods with side effects when you have a specific need and justification for them.

It is explained on previous pages why to prefer vals, immutable objects, and methods without side effects so this sentence makes perfect sense.

But second sentence:"Use vars, mutable objects, and methods with side effects when you have a specific need and justification for them." is not explained so well.

So my question is:

What is justification or specific need to use vars, mutable objects and methods with side effect?


P.s.: It would be great if someone could provide some examples for each of those (besides explanation).

like image 616
PrimosK Avatar asked Feb 01 '12 10:02

PrimosK


People also ask

What is imperative programming style?

Imperative programming is a software development paradigm where functions are implicitly coded in every step required to solve a problem. In imperative programming, every operation is coded and the code itself specifies how the problem is to be solved, which means that pre-coded models are not called on.

What are some differences between imperative and functional programming styles?

The difference between functional programming and imperative programming is that functional programming considers the computations as mathematical functions and avoids changing state and mutable data while imperative programming uses the statements that change the programs state.

Is Java imperative or declarative?

Declarative Programming Languages: Examples of imperative languages are Pascal, C, Java, etc. Examples of declarative languages are ML, pure Lisp and pure Prolog. The programming model in imperative languages is based on a statement-at-a-time paradigm where each statement has some effect on a memory store.

Is functional programming declarative or imperative?

Functional programming is a form of declarative programming that expresses a computation directly as pure functional transformation of data. A functional program can be viewed as a declarative program where computations are specified as pure functions.


1 Answers

In many cases functional programming increases the level of abstraction and hence makes your code more concise and easier/faster to write and understand. But there are situations where the resulting bytecode cannot be as optimized (fast) as for an imperative solution.

Currently (Scala 2.9.1) one good example is summing up ranges:

(1 to 1000000).foldLeft(0)(_ + _)

Versus:

var x = 1
var sum = 0
while (x <= 1000000) {
  sum += x
  x += 1
}

If you profile these you will notice a significant difference in execution speed. So sometimes performance is a really good justification.

like image 85
Heiko Seeberger Avatar answered Sep 19 '22 04:09

Heiko Seeberger