Skip to main content

Questions tagged [side-effect]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
14 votes
4 answers
3k views

What side-effects, if any, are okay when importing a python module?

Generally, modules should not have side effects. However, in a lot of cases, the side-effects are hard to avoid or may be desirable. There are also popular packages with on-import side-effects. Which ...
Kaia's user avatar
  • 412
10 votes
6 answers
5k views

If a function mutates outer state during execution but reverts the outer state into original state after execution, does it still contain side effect?

According to What is a "side effect?", I know the side effect means changing outside world. But what if a function that changes the outside state during execution, but reverts the state to ...
wcminipgasker2023's user avatar
1 vote
5 answers
603 views

Should I repeat calculations twice to follow "return a value or have side-effects, but not both"?

According to Origin of "a method should return a value or have side-effects, but not both", a method should either return a value or have side-effect, but not both. However, I often see some ...
wcminipgasker2023's user avatar
3 votes
1 answer
166 views

How to handle a state machine side effect being optional?

I have a state machine that, as a side effect of going into a certain state, sends a message to a remote server. In some situations, however, I don't want the state machine to send that message, even ...
PieterV's user avatar
  • 215
0 votes
3 answers
562 views

How to implement state machine side effect that needs data the state machine doesn't have

Given a state machine that implements a certain protocol. I need multiple instances of this protocol running, so I create multiple instances of this state machine. Each instance is associated with a ...
PieterV's user avatar
  • 215
17 votes
5 answers
6k views

Is there a non-deterministic function without side effects?

By definition, a pure function is deterministic + no side effect. Is there any example for a function which has no side effects, but is non-deterministic? I.e., a function without side effects, but ...
Helin Wang's user avatar
0 votes
1 answer
916 views

How to test a function with several conditional nested side effects

In Python, consider a function like the following: def main(*args): value1 = pure_function1(*args) if condition(value1): value = side_effect1(value1) if value: ...
pob's user avatar
  • 109
-4 votes
2 answers
135 views

Does the HTTP specification fully define the semantics of request methods?

I see two possible interpretations of the semantics of request methods defined in the HTTP specification RFC 7231: The intended effect of a request method is fully defined by the HTTP specification (...
Géry Ogam's user avatar
0 votes
2 answers
231 views

A command as the intended effect of POST versus as the side effect of PUT

The intended effect (semantics) of the POST method is resource specific, e.g. executing a command with arguments: POST /command HTTP/1.1 {"parameter-1": "argument-1", "...
Géry Ogam's user avatar
3 votes
2 answers
451 views

Is rebooting a server idempotent or not?

In his article RESTful Casuistry, Tim Bray claims that rebooting a server is not idempotent: But I don’t buy it, and here’s why. If I want to update some fields in an existing resource, I’m inclined ...
Géry Ogam's user avatar
2 votes
1 answer
332 views

Can I enforce "functional core, imperative" shell with a framework?

The design pattern known as "functional core, imperative shell" is about separating side-effects from pure calculations, where business logic is supposed to be pure and then coordinated by ...
Olle Härstedt's user avatar
1 vote
1 answer
1k views

Is having side-effects in constructor an anti-pattern?

I was trying to understand how to embed v8 engine in a C++ application and was trying to understand the following hello world problem as a result. I found the code to be unreadable at first glance. e....
Anurag Kalia's user avatar
1 vote
2 answers
495 views

Remove all side-effects from business logic

I'm looking for feedback for a design pattern that aims to remove all side-effects from business logic. I'm using PHP but the pattern can be applied to any OOP language. The point is to enforce pure ...
Olle Härstedt's user avatar
2 votes
2 answers
693 views

What's the value of IO Monad?

When I'm writing code in the form of IO Monad, I wonder what's real value of it.For example I have a function as def something(in: In): IO[Out]; my function is a pure function that **returns an impure ...
Reza Same'ei's user avatar
8 votes
3 answers
888 views

Avoiding side effects in immutable class constructor

I rewrote a very long method in which some data is queried from a database, based on info about a particular account, which is queried first. I split out the account info into an immutable inner ...
jbruenker's user avatar
0 votes
1 answer
546 views

Side-effect-free functions and returning domain information DDD

In Domain-Driven Design (Eric Evans) the discussion on side-effect-free functions talks about separating commands which "result in modifications to observable state" from queries which have no side-...
richflow's user avatar
  • 103
0 votes
1 answer
224 views

Where to store side effect state in an event sourcing system

I also had a look at How do I deal with side effects in Event Sourcing? but the solution wasn't clear to me. If I store "EmailSent" event in the event stream, I might issue the external request to ...
Mihail Malostanidis's user avatar
0 votes
3 answers
464 views

Side-effect free programming language for reproducible data transformation

Is there a usable programming language that disallows all side effects except for its input stream (aka STDIN) and its output stream (aka STDOUT)? All executable scripts in the language should be ...
Jakob's user avatar
  • 647
1 vote
1 answer
197 views

best practice for data.table use in "formal" code

Consider a "large-ish" data set (~2-5M rows) that goes through multiple stages of cleaning/processing: library(dplyr) largedat %>% mutate( # overwrite v1 based on others v1 = somefunc(...
r2evans's user avatar
  • 336
11 votes
3 answers
5k views

Do functional programming languages disallow side effects?

According to Wikipedia, Functional programming languages, that are Declarative, they disallow side effects. Declarative programming in general, attempts to minimize or eliminate side effects. Also, ...
codebot's user avatar
  • 221
49 votes
9 answers
13k views

Return considered harmful? Can code be functional without it?

OK, so the title is a little clickbaity but seriously I've been on a tell, don't ask (TDA) kick for a while. I like how it encourages methods to be used as messages in true object-oriented fashion. ...
candied_orange's user avatar
7 votes
4 answers
6k views

Does object-oriented programming allows side effects and state changes?

I know that obviously every language has its own characteristics and implementations, but from a theoretical standing point, viewing OOP as an abstract programming paradigm, does it allows side ...
slevin's user avatar
  • 179
8 votes
6 answers
749 views

Why is it is easier to reason about programming languages and programs that have no side effects?

I read "The Why of Y" from Richard P. Gabriel. It is an easy to read article about the Y combinator, which is quite rare. The article begins with the recursive definition of the factorial function: ...
ceving's user avatar
  • 391
3 votes
0 answers
175 views

Are there downsides to writing PDO/mysqli statements into my code directly vs using my own redirect wrapper methods?

TL;DR: To access DB functionality in my codebase I currently use a custom layer throughout the code. The layer needs an upgrade in order to begin accepting prepared statements, which it currently ...
Dennis's user avatar
  • 8,247
48 votes
7 answers
26k views

What do you call a function where the same input will always return the same output, but also has side effects?

Say we have a normal pure function such as function add(a, b) { return a + b } And then we alter it such that it has a side effect function add(a, b) { writeToDatabase(Math.random()) return a ...
m0meni's user avatar
  • 803
20 votes
5 answers
3k views

Is the benefit of the IO monad pattern for handling side effects purely academic?

Sorry for yet another FP + side effects question, but I couldn't find an existing one which quite answered this for me. My (limited) understanding of functional programming is that state/side effects ...
Stu Cox's user avatar
  • 311
17 votes
4 answers
4k views

Origin of "a method should return a value or have side-effects, but not both"

I read once that a method should either have a return value (and be referentially transparent), or have side-effect(s), but not both. I cannot find any references to this rule, but want to learn more ...
Wayne Conrad's user avatar
  • 1,138
1 vote
2 answers
301 views

Are any side effects not concrete side effects?

In Chapter 23 of "Object Oriented Software Construction" (1988), Betrand Meyer makes a distinction between side effects, concrete side effects, and abstract side effects. Meyer defines a side effect ...
Ceasar's user avatar
  • 559
6 votes
1 answer
824 views

Mutable with logic inside or immutable with logic outside?

I know immutable objects are preferred to mutable objects for reasoning and maintenance. But in occasions making a class immutable have some costs, let me explain it with a simple example: class ...
Ahmad's user avatar
  • 1,836
0 votes
1 answer
71 views

Choose approaches for updating an object

Say I have a simple object created by from user input: var input = { url: 'http://example.com/', path: 'abc', user: 'adam' }; And I am to write function(s) that update url and path properties ...
bitinn's user avatar
  • 101
9 votes
1 answer
3k views

Functional programming for loop side effect

I am trying to get my head around as to why having a local variable or a for loop inside a function is not considered to be pure functional programming. Given this function: int as_int(char *str) { ...
tomatoRadar's user avatar
9 votes
6 answers
6k views

How can IO cause side effects in Functional Programming? [duplicate]

Whenever I read about Haskell, I find that IO can cause side effects. But I do not understand how it would do it? Do we mean that we are writing to a file from one lazy sequence and another lazy ...
Ashish Negi's user avatar
19 votes
5 answers
5k views

Why is reading from memory not a side-effect but reading from a file is?

What does exactly make reading from the process memory a pure operation? Suppose I created an array of 100 integers in the global memory and then took the 42th element of this array. It is not a side ...
ZhekaKozlov's user avatar
8 votes
2 answers
2k views

Alternative to language purity

Purity One of the interesting concepts in Haskell is the purity. However, I am wondering what the pragmatic reasons behind this is - let me explain a bit more before you reject my question. My main ...
nilu's user avatar
  • 1,024
11 votes
4 answers
1k views

Is it ever OK for a conditional to have side effects? [closed]

I'm taking intermediate data structures course as a prereq for entry into the CS MS program at a University everyone in America has heard of. One line of code that was written in class caught my eye: ...
rianjs's user avatar
  • 220
7 votes
2 answers
1k views

Can referentially transparent functions have side effects?

I read everywhere that referential transparency and side-effects are mutually exclusive for all functions, however, what about the case in which a function changes some state that has no effect on its ...
swanyboy's user avatar
12 votes
3 answers
3k views

Side Effects Breaking Referential Transparency

Functional Programming in Scala explains a side effect’s impact on breaking referential transparency: side effect, which implies some violation of referential transparency. I’ve read part of SICP, ...
Kevin Meredith's user avatar
9 votes
2 answers
2k views

Unit testing side effect-heavy code

I'm starting to write C++ code to run a robot, and I don't know how to incorporate unit testing, if indeed I can. I have been provided with a library which allows the creation of "commands" ...
Rose Kunkel's user avatar
17 votes
2 answers
557 views

Side effect-free interface on top of a stateful library

In an interview with John Hughes where he talks about Erlang and Haskell, he has the following to say about using stateful libraries in Erlang: If I want to use a stateful library, I usually build ...
beta's user avatar
  • 1,012
11 votes
2 answers
918 views

Where do we put "asking the world" code when we separate computation from side effects?

According to Command-Query Separation principle, as well as Thinking in Data and DDD with Clojure presentations one should separate side effects (modifying the world) from computations and decisions, ...
Alexey's user avatar
  • 1,229
10 votes
3 answers
2k views

How is referential transparency enforced?

In FP languages, calling a function with the same parameters over and over again returns the same result over and over again (i.e. referential transparency). But a function like this (pseudo-code): ...
JohnDoDo's user avatar
  • 2,309
10 votes
2 answers
4k views

Are side-effects in Array's "every", or "some" bad?

I've always been taught that having side-effects in an if condition are bad. What I mean is; if (conditionThenHandle()) { // do effectively nothing } ... as opposed to; if (condition()) { ...
Isaac's user avatar
  • 203
1 vote
2 answers
618 views

I don't understand the definition of side effects [duplicate]

Possible Duplicate: What is a “side effect?” I don't understand the wikipedia article on Side Effects: In computer science, a function or expression is said to have a side effect if, in ...
Chris Okyen's user avatar
30 votes
3 answers
7k views

Different ways to see a monad

While learning Haskell I have faced a lot of tutorials trying to explain what are monads and why monads are important in Haskell. Each of them used analogies so it would be easier to catch the meaning....
Oni's user avatar
  • 957
7 votes
6 answers
707 views

Does long term programming affect writing in a native spoken languages? [closed]

So, I am getting some years under my belt in long term programming. I have noticed, however, when the frequency and duration started increasing in my coding sessions that when I actually went to ...
Chad Harrison's user avatar
14 votes
5 answers
2k views

How to create scalable & side-effect free integration tests?

In my current project, I am having a hard time coming up with a good solution to create scalable integration tests that have no side effects. A little clarification on the side effect free property: ...
Guven's user avatar
  • 924
37 votes
7 answers
8k views

Asynchronous Programming in Functional Languages

I'm mostly a C/C++ programmer, which means that the majority of my experience is with procedural and object-oriented paradigms. However, as many C++ programmers are aware, C++ has shifted in emphasis ...
Charles Salvia's user avatar
80 votes
14 answers
35k views

Why are side-effects considered evil in functional programming?

I feel that side effects are a natural phenomenon. But it is something like taboo in functional languages. What are the reasons? My question is specific to functional programming style. Not all ...
Gulshan's user avatar
  • 9,462