1

According to Wikipedia, "an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, which is to say if it has any observable effect other than its primary effect of returning a value to the invoker of the operation."

This is the most used definition, but some professionals (as well as the Wikipedia article, if we read it more carefully further) also say that "A side effect is when a function relies on, or modifies, something outside its parameters to do something." This is technically reasonable and correct; however, it looks non-logical with respect to the human language.

English Wiktionary gives several definitions for the word "effect" (I'm not a native speaker), but all of them are connected to result, not input; the word comes from Latin ex (“out”) + faciō (“do, make”).

What could be an alternative term for a function that only relies on some external state? It is unimportant for me whether the function actually makes some external modifications; I want to say the opposite to: it always gives same results when getting the same input.

7
  • "What could be an alternative term for a function that only relies on some external state?" Bad design ;) Commented Jul 21, 2023 at 14:36
  • @PhilipKendall I don't think this is always bad design. It could read a value from the database. This is not an answer. Commented Jul 21, 2023 at 15:42
  • Independence covers it. A function that has no side effects and does not rely on external state is said to have no dependencies, is said to be independent of its environment. Commented Jul 22, 2023 at 7:55
  • @MartinMaat thank you, this sounds like a good term, but it is not often used by programmers. Commented Jul 23, 2023 at 20:19
  • 1
    It might be a pure function returning a sequence of values. Where the sequence might be infinite, with values computed on demand. But it also depend on things like what "observable" means. Is logging "observable"? It depends on the point of view.
    – JonasH
    Commented Jul 24, 2023 at 13:49

3 Answers 3

1

You're looking for something like the term pure function vs. impure function.

While the wikipedia article tends to focus on return values directly derived from non-local variables, consulting the non-local variable in some decision would also make the function impure.

In computer programming, a pure function is a function that has the following properties:

  1. the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and
     ...

...

The following C++ functions are impure as they lack the above property 1:
  ...

  • because of return value variation with a non-local variable

...

    int f() {
      return x;
    }

However, these definitions apply to functions that rely on some external state, rather than relying only on external state — but from the computer science and functional perspective, relying on any external mutable state is at the issue.  Relying on immutable state doesn't violate purity whether external or internal.

(In most environments, code itself is immutable state, so calling another pure function doesn't violate purity even if the function is external as long as the external function is also pure).

1
  • This makes perfect sense, many thanks. Upvote and accept. "rather than relying only on external state" - no, this was never needed; of course, a function can always accept arguments. Commented Jul 21, 2023 at 15:47
1

What could be an alternative term for a function that only relies on some external state?

Because you said "only", there is no single term for this. There is a term for "relies on some external state". It's called having a

But if you really want that "only" then you need to add:

Which just means the function takes no arguments.

As for pure functions, they have two requirements:

  • Anything that changes their behavior must be explicitly passed in. That makes them referentially transparent. Calculate that f(2) gives 4 once and you can replace every f(2) with 4 so long as f is unchanged.
  • Anything (observable) they change must only be what they return. Side effect free. f might also put 4 on your printer but so long as the program can't observe that we don't care. However, that will stop working the same way if the program starts replacing f(2) with 4 without calling f.

What you're scratching at is things that change the behavior of f that were not passed in. That invites debate on what is part of f.

If f uses a constant most people will think it's still pure. But constants get changed. So is that constant a hidden dependency or is it part of f? Is changing the constant simply changing f? Or since f looked at a constant defined outside of f, was f never pure?

1
  • This is also an excellent answer, many thanks for new important concepts! I've heard of many of them, but failed to remember when it was needed :) Commented Jul 23, 2023 at 20:16
1

I think that there can only be real "side effects" if there is both reading and writing of contextual data.

It's stretching credulity to argue that reading a contextual constant is a "side effect", and of course writing a contextual variable without anything ever reading it would be both pointless and clearly have no "effect".

So a "side effect" must involve a transmission of data by means other than the arguments and results of functions.

Personally I think the whole discourse around this matter tends to damage minds, because no reasonable program is supposed to have only internal effects, no reasonable system is supposed to have only immutable data, and the consequence of trying otherwise is only grotesquely distorted and long-winded code.

The language is clearly chosen to be rhetorical. Nobody likes "impure" food, do they? Nobody likes "side effects" of medications, either?

But in fact in computing we do like a program to interact with its context, and what we always want to achieve is an organised and judicious interaction, not an absence of interaction.

Returning to the question of what to call a function which reads contextual variables but doesn't write them, an example of such a function is one that returns the current time.

Typical words for this are "non-deterministic" or "volatile" - in the sense that the results of the call are not fully determined by the arguments (if indeed there are any arguments).

These words don't specifically exclude the possibility that data may be written out to context, but it does make clear that something is being read in from an external context.

I would add though that analysing each and every function in terms of whether it reads context or not, is usually a sign of being in that damaging mindset of seeking functional "purity".

Except for specialist components that don't form whole programs (such as components dealing with highly numerical work), you would not normally need to be concerned with whether a function interacts with context at all, only with how organised and orderly that interaction is. If a function happened to be pure, that would just be an unremarkable coincidence, not an important fact to note and distinguish from others.

2
  • 1
    "side effects" if there is both reading and writing of contextual data" - only in a very general sense. It could return different values for different environmental variables (that is not "writing" them somewhere else, as is usually dubbed side effects). Your answer is also great, I upvote and maybe accept. Commented Jul 23, 2023 at 20:33
  • As I said, I'm interested only in input, that is why it is more terminology/emphasis than real code analysis. My task is the following: I capture initialization parameters of a user's class and say that two objects are equal iff their initialization parameters are the same (they don't have comparison operators, they could be generators from Python standard libraries). Of course, I warn users that they should be aware that it won't always work. Then functions printing time would still be the same. However, if they secretly get time, store it, and then use it, then they'll be different. Commented Jul 23, 2023 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.