[erlang-questions] Re: Concept of Side-effect
Fri Sep 18 04:48:52 CEST 2009
Kaiduan Xie wrote:
> Benjamin Tolputt wrote:
>> Kaiduan Xie wrote:
>>> Thanks a lot for the great examples. But I can not understand the following one,
>>> add(X,Y) ->
>>> add_effects(X,Y) ->
>>> What side-effect add_effects generates? Why we say io:format()
>>> generates side-effect?
>> The side-effect is that there is something printed to the console (or
>> stdout). If there were another function (say 'print_screen') that could
>> look at the screen, it is possible/likely a call to the add_effects
>> function would change the output of the function.
> Thanks Benjamin, but sometimes printing out to the console is really
> what you want. Maybe this is not a good example :)
Oh there is no debating that you may want that behaviour just, that in
having the side-effect of printing to the screen, you then need to be
aware of other functions that use that state (i.e. the example
Side-effects are not necessarily bad things - they just add to the
things you need to keep a track of. The less side-effects there are, the
more "local" you can limit your thoughts when coding new functionality.
Whilst I use Erlang for the things I can, I do a majority of my coding
in C/C++. A majority of the times I have found bugs, it has been
state-changing functionality (i.e. side-effects) in code I was unaware
of. Erlang makes it harder to create these side-effects by limiting the
ways in which it can be done :)
P.S. Hope you don't mind my re-arrangement of your email - the "reply at
top" made it harder to follow the flow of the email.
More information about the erlang-questions