Tag Archives: side effects

Side Effects in programming

According to Wikipedia, a function or expression is said to have a side effect if it modifies some state outside its local environment. Let’s understand this in depth by examples.

To understand side effects, first, we should know what “state” is. In layman terms, the state refers to the value of the variable at any given point in the program like x=2.

Now, the above Wikipedia definition might have started to make some sense. If not, don’t worry, see the figure below.

In this figure, we have a variable x that is outside the scope of two functions. Clearly, after the Function_2 call, the value of x changes which means that Function_2, other than returning value(if any), is interacting with variables outside its local environment and thus has a side effect.

In general, any lasting effect that occurs in a function, not through its return value, is called a side effect. Not through its return value means if we assign x=Function_1(), this will clearly change the value of x but this is not a side effect. For side effect, the change should occur inside a function.

Common ways to have side effects:

  1. Modifying a non-local variable: Let’s understand by an example

    The x in the square() function is local to its scope so no change in outside x. Global in square_side_effect() makes x global so any change in x will change it in whole code (outside function scope) so the square_side_effect function() is not pure(has side effects).
  2. Changing the value of a mutable object: Let’s take Lists which is a mutable object.

    Clearly, after calling list_side_effect(), the value of lst changes so, list_side has side effects.
  3. Performing Input/Output operations: This doesn’t change any objects or variable value, but it does have a potential lasting effect outside the function call because a person might see the output and be influenced by it.
  4. Calling other side-effect functions

Now, you might have got some feeling about the side effects. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Functional programming paradigm

Functional programming is based on Lambda Calculus, a computational model proposed by Alonzo Church in the 1930’s. This paradigm falls under the declarative umbrella rather than imperative, meaning, programming is done with expressions or declarations instead of statements. (Relax… See below the properties and you will understand what this means)

Note: We can do programming in a functional style in languages that are not specifically designed for functional programming. C++, Java, Python etc. all added constructs to facilitate the functional style. So, programmers using “mostly imperative” languages may have utilized some of the functional concepts.

Pros: more concise, more predictable, and easier to test than imperative or object-oriented code only if you understand.

Main Concepts of Functional Programming:

  1. Pure Functions: A pure function is a function which follows given properties like
    • For the same input, it always returns the same output.
    • Has no side effects(For details check the link).
    • Referential Transparency.

    Although, these properties are somehow correlated. Like, a function can only return the same output for the same input, only if it has no side effects. Let’s see by examples

    The square function always gives the same output for the same x .

    Even though we didn’t pass any arguments to the random_num function, it gives different output, meaning that random_num is not pure.

    Now, let’s look at the side effect property.
    Side effect is any state(value of the variable) change outside the called function scope. In simple terms, if a function changes the state/value of the variable that is outside function scope or local environment then the function/unit of code is said to have a side effect.

    Clearly, the x value changes after square_side() function which means that this function has side effects. The x in the square() function is local to its scope so no change in outside x. Global in square_side() makes x global so any change in x will change it in whole code (outside function scope) so the square_side function is not pure(has side effects).

    Another important property of pure functions is Referential Transparency means we can replace a function call with its resulting value without changing the meaning of the program. Let’s see by an example

    In the above example, we can easily replace double(2) by 4 because we know that this function always gives the same result for the same input(No side effects). This is Referential Transparency.

  2. First Class functions: “first-class” is a computer science term that describes something that has no restriction on its use. Thus, first-class functions mean it can appear anywhere in the program like as arguments to other functions and as their return values.
  3. Recursion: Recursion means letting an operation be repeated until it reaches the base case. In the functional paradigm, we do not use the loops and all the work is done by recursion.

  4. Avoid Shared State:  A shared state is a state that is shared between more than one function or more than one data structure and you should avoid creating functions that rely on the same variable.

Now, you might have got some feeling about the Functional programming. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Imperative vs Declarative programming paradigm

In this post, we will be looking at the major differences between imperative and declarative programming paradigms.

Imperative Programming paradigm: The languages that fall into this category have 2 main properties:

  1. Control flow is explicit meaning how the operation took place, step by step or more naively “First do this then do that”.Thus, the order of steps is crucial.
  2. Allow side effects.

To understand side effects, first, we should know what “state” is. In layman terms, the state refers to the value of the variable at any given point in the program like x=2.

Side effect is any state change outside the called function scope. In simple terms, if a function changes the state that is outside function scope or local environment then the function/unit of code is said to have a side effect.

Examples of function side effect include modifying any external variable or object property, performing I/O, calling functions having side effects etc.

Let’s see how this code follows the properties of imperative paradigm. First, the order of steps is crucial because if we implement “mean_fare” statement before the loop, it will give a different result. Second, the unit of code i.e. loop, has side effects as it changes the state of the “Total” variable which is outside its scope.

I hope this might have given you the insight of the imperative programming paradigm.

Now, let’s look at some contradictions that I found while searching for this stuff

Some people might argue that the above code belongs to structured programming which is a kind of imperative programming. The main difference between these two is that in the imperative, control flow is defined by the collection of “goto” statements while in the structured, control flow is defined by nested loops, conditionals, and subroutines with no “goto” statements. okay that’s good I said, but then they categorized “Python” into imperative paradigm knowing that it doesn’t have “goto” statement.

So, what I found is that there are terms like structured and non-structured imperative programming. Above code falls into the structured imperative paradigm while if we use the goto statement to define control flow then it falls under the non-structured imperative paradigm.

Declarative Programming paradigm: This is defined as any style of programming that is not imperative. So, the properties will just be the opposite of imperative.

  1. Sequence of the steps is not crucial.
  2. Don’t allow Side effects.

In general, the imperative focuses on “how to do” while declarative focuses on “what to do”meaning logic of software without actually describing its flow. Let’s look at the code below to get more insights into this:

Using reduce() and lambda functions

Note: Since python in no sense is declarative, the above examples are more towards functional side than declarative. But since functional also falls under the declarative umbrella, above examples can serve as a good platform for understanding declarative concepts.

Explanation: In the above examples, we are describing “WHAT” we want to do rather than HOW (we don’t know HOW lambda and reduce are implemented, we also don’t care). We’re not mutating any state. All of the mutations are abstracted inside of “lambda” and “reduce. It’s also more readable (once you get used to “lambda” and “reduce, of course).

Now, you might have got some feeling about the “How to do” and “What to do” approach. Later, this will become more clear when we will go through Functional and other paradigms. Till then, Enjoy.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.