Skip to main content
107 votes
Accepted

Return considered harmful? Can code be functional without it?

If a function doesn't have any side effects and it doesn't return anything, then the function is useless. It is as simple as that. But I guess you can use some cheats if you want to follow the letter ...
JacquesB's user avatar
  • 62.4k
43 votes
Accepted

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

Well, at least it is a temporary side effect. You may notice the difference to the fully side-effect free version when you run your program in a multi-threaded context. Since your case involves ...
Doc Brown's user avatar
  • 220k
40 votes

Return considered harmful? Can code be functional without it?

Tell, Don't Ask comes with some fundamental assumptions: You're using objects. Your objects have state. The state of your objects affects their behavior. None of these things apply to pure functions. ...
Robert Harvey's user avatar
39 votes

Is there a non-deterministic function without side effects?

Of course this depends on the definitions. Let's drop "pure" which has a definition in your question that clearly makes non-deterministic pure functions impossible as being deterministic is ...
Hans-Martin Mosner's user avatar
30 votes

Return considered harmful? Can code be functional without it?

When I deal with an object I don't ask it about its internal state. I tell it what I need to be done and it uses its internal state to figure out what to do with what I've told it to do. You don't ...
Timothy Truckle's user avatar
29 votes
Accepted

Do functional programming languages disallow side effects?

Functional programming includes many different techniques. Some techniques are fine with side effects. But one important aspect is equational reasoning: If I call a function on the same value, I ...
amon's user avatar
  • 136k
26 votes
Accepted

Is there a non-deterministic function without side effects?

A properly working classical computer is, by definition, deterministic. That is, the output of the same series of steps with the same inputs will produce the same result. When we talk about non-...
JimmyJames's user avatar
  • 30.9k
23 votes

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

logging # Creates a logfile logging.basicConfig(filename="module.log") No, don't do it! Now git status is dirty, it shows an untracked file. Protect this with a __main__ guard, or let ...
J_H's user avatar
  • 7,891
19 votes

Return considered harmful? Can code be functional without it?

If you consider return as "harmful" (to stay in your picture), then instead of making a function like ResultType f(InputType inputValue) { // ... return result; } build it in a ...
Doc Brown's user avatar
  • 220k
13 votes

Is there a non-deterministic function without side effects?

There is no need to use some special hardware like a clock or sensors to give an example. The point is, any function which return value can be influenced by a side effect of other functions is non-...
Doc Brown's user avatar
  • 220k
13 votes

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

However, in a lot of cases, the side-effects are hard to avoid or may be desirable. They are never hard to avoid, and they are never desirable. Or maybe they somewhat are hard to avoid, because it ...
freakish's user avatar
  • 3,085
12 votes
Accepted

Avoiding side effects in immutable class constructor

Some thoughts: Now, as far as I know, it's bad practice to have effectful computations inside a constructor That depends on what you mean by "effectful." The purpose of a constructor is to ...
Robert Harvey's user avatar
9 votes
Accepted

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

I am not sure if there is legitimate use-cases for such code patterns. Yes there are, and such stuff is even used in the c++ standard library. Prominent examples are the std::lock_guard and any kind ...
πάντα ῥεῖ's user avatar
8 votes

Return considered harmful? Can code be functional without it?

Message passing is inherently effectful. If you tell an object to do something, you expect it to have an effect on something. If the message handler was pure, you would not need to send it a message. ...
Bergi's user avatar
  • 1,363
8 votes

Avoiding side effects in immutable class constructor

"Classes should be immutable unless there's a very good reason to make them mutable." Joshua Bloch - Effective Java   "Principles should be followed because you understand what you're ...
candied_orange's user avatar
7 votes
Accepted

Is rebooting a server idempotent or not?

The problem here is that Tim Bray doesn't explain why he thinks it's not idempotent. The concept of idempotency is somewhat subjective. When we call a PUT twice there are a number of things that ...
JimmyJames's user avatar
  • 30.9k
7 votes

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

The trouble with trying to follow everybody's idea of what is good coding principles is that you end up unable to actually write anything without violating at least one of those principles. Repeating ...
Simon B's user avatar
  • 9,782
6 votes
Accepted

What's the value of IO Monad?

In addition to what Jacques said about the side effects being explicit in your type system, you also get referentially transparent side effects. This means you can store effects or groups of effects ...
Karl Bielefeldt's user avatar
6 votes

What's the value of IO Monad?

The primary benefit is you get the IO explicitly in the type of the function. So you know that any function without IO in the signature is side-effect free. The pure parts of the program is guaranteed ...
JacquesB's user avatar
  • 62.4k
6 votes
Accepted

Is it OK for a function to both do-something and return-something?

Command-Query separation is surely a useful principle, but it should not be taken as a dogma. Fowler once wrote Meyer likes to use command-query separation absolutely, but there are exceptions. ...
Doc Brown's user avatar
  • 220k
5 votes

Do functional programming languages disallow side effects?

No programming language eliminates side effects. I think it's better to say that declarative languages contain side effects while imperative languages do not. However, I'm not so sure that any of this ...
Daniel T.'s user avatar
  • 3,073
5 votes

Return considered harmful? Can code be functional without it?

This entire question strikes me as a 'level violation'. You have (at least) the following levels in a major project: The system level e.g. e-commerce platform The sub-system level e.g. user ...
Jared Smith's user avatar
  • 1,935
5 votes

How to handle a state machine side effect being optional?

I need to somehow block the state machine Implementing semaphores in your state machine increases complexity considerably and introduces cases for which the state machine might not be prepared (...
Laiv's user avatar
  • 15k
5 votes

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

It is not side effect free anymore. Even if you try to cleanup the "temporary modifications", it is a visible side effect. You may get away with it in some situations, of course, but it can ...
schlenk's user avatar
  • 209
4 votes

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

My (limited) understanding of functional programming is that state/side effects should be minimised and kept separate from stateless logic. That's not just functional programming; that's usually a ...
cjs's user avatar
  • 783
3 votes

Side-effect free programming language for reproducible data transformation

Check out Total Functional Programming which mentions Epigram and Charity. Haskell is the first that comes to mind, having the best combination of purity and popularity. You can also search for "...
GlenPeterson's user avatar
3 votes

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

If I PUT a resource representation, then the expectation is that a GET of the same resource returns an equivalent representation. For example, if I PUT /command with body print("hello world")...
amon's user avatar
  • 136k
3 votes
Accepted

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

I don't think the RFC is attempting to define "intent" and "side effect"; rather, it is referring to them as already existing concepts in the minds of implementers. If you have an ...
IMSoP's user avatar
  • 5,957
3 votes

How to test a function with several conditional nested side effects

Some quick things to mention: The short answer here is to not write code with so many side effects. If you build a complex algorithm, it'll be more complex to manage. Don't build something you don't ...
Flater's user avatar
  • 59.5k
3 votes

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

Use the state machine as a "controller", in the MVC sense: have it emit commands (call methods or send messages) to some other piece of code which does have the right information. The ...
pjc50's user avatar
  • 15.3k

Only top scored, non community-wiki answers of a minimum length are eligible