3 Replies - 881 Views - Last Post: 25 July 2019 - 12:56 PM

#1 malvinbozniak   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-July 19

How do exactly imperative statements introduce side effects ?

Posted 24 July 2019 - 05:42 PM

Hi there !

I'm learning functional programming.
I found it fascinating, but there are some definitions that confuse me sometimes.
For example when I read things like "imperative statements introduce side effects" ...
I understand that if an imperative program writes global data, it is introducing side effects, but ...
everybody knows that writing global data is a bad practice, so nobody would do that anyway.
I mean, that definition could let somebody with the impression that imperative languages work that way, but it's just a declared bad practice that hopefully nobody follow.

Are there other ways to demostrate that imperative statements introduce side effects ?

Regards !

Is This A Good Question/Topic? 0
  • +

Replies To: How do exactly imperative statements introduce side effects ?

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7492
  • View blog
  • Posts: 15,527
  • Joined: 16-October 07

Re: How do exactly imperative statements introduce side effects ?

Posted 25 July 2019 - 04:16 AM

View Postmalvinbozniak, on 24 July 2019 - 07:42 PM, said:

I understand that if an imperative program writes global data, it is introducing side effects, but ...

Good, but, writing to global isn't the side effect, a program segment expecting that global is in one state and getting an unexpected state is the side effect.

A side effect is the bugbear of multiple threads. The issue is mutability. An imperative block of code mutates its own state as a matter of course (e.g. normal loop.) This is fine; it's how it works. However, if that block gets executed more than once at the same time, will that mutating state offer expected results to the calling processes or will it step on itself introducing side effects?

Most code, imperative or otherwise, is not written with thread safety in mind. Some languages (compilers) enforce thread safety with varying success. However, pure functional languages give you thread safety for free, due to lack of mutability, which is one of the reasons they've experienced something of a renaissance.
Was This Post Helpful? 0
  • +
  • -

#3 malvinbozniak   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-July 19

Re: How do exactly imperative statements introduce side effects ?

Posted 25 July 2019 - 10:40 AM

View Postbaavgai, on 25 July 2019 - 04:16 AM, said:

Good, but, writing to global isn't the side effect, a program segment expecting that global is in one state and getting an unexpected state is the side effect.


Alright, so side effects are introduced not by using global data, but to do it without the necessary care.
But changing data external to a function isn't a side effect, wheter it generates a problem or not ?
I didn't read something emphasizing this point so far, but I think you can be right.
Thank you !
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7492
  • View blog
  • Posts: 15,527
  • Joined: 16-October 07

Re: How do exactly imperative statements introduce side effects ?

Posted 25 July 2019 - 12:56 PM

Whether a side effect is considered detrimental or not is a function of context. That seemed to be where you were leaning, so I went there. But "side effect" is not exclusive to unintended effects. Sorry for the confusion.

Many languages have the ++ operator, for instance. This is essentially a side effect by design. You can read and change a value in single declaration.

In OOP, methods operate in the context of the state of an object. In most languages, how a certain method impacts an object's state needn't be explicit. C++ offers a const guard to ensure a method doesn't change things, but even then such "const correctness" is at the whim of the programmer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1