I’ve seen a couple posts in the top in the last 6 hours feed, and it seems like people are really up in arms about this functional programming stuff. Not really sure what it even is.

It looks like it’s people writing bad programming or something? Like a lot of extra stuff that is not necessary?

EDIT: sorry everyone, I’m not a programmer and I don’t know to much other than a little java and python. I guess I should have posted this in Explain Like I’m Five.

  • dfyx@lemmy.helios42.de
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    5 months ago

    Let me try getting this to ELI5 levels:

    Most programming languages use a way of programming called “procedural programming” (or a variation of that, for example object-oriented programming). A series of instructions gets executed step by step to modify the program’s overall state. Imagine it like a cooking recipe. You have control structures like if, while and for that influence the order in which instructions are executed. There are functions (or methods in object-oriented programming) that let you reuse some instructions over and over but that’s not quite functional programming.

    I’ll use a syntax similar to Java, C++ or C# as an example:

    void squareAllElements(int[] array)
    {
        for(int i = 0; i < array.size(); i++)
        {
            array[i] = array[i] * array[i];
        }
    }
    

    Functional programming is a bit more like what you know from maths class. There are still functions that take a list of inputs and produce an output but there is no state to be modified. Every operation must take everything it needs (apart from maybe constants) as parameters and produces new values/objects as an output. You say you know Java, so imagine that none of your methods returns void and every variable can only be assigned once. There are no loops but for example operations that let you separate the first element of a list from the rest or create a new list by applying an operation to all elements of an input. To make that easier, functions can take other functions as parameters.

    So let me show the same example in a functional style. This is no specific language, just an example to help you imagine how it works.

    int square(int x) => x * x;
    int[] squareElements(int[] array) => applyToAllElements(array, square);
    

    Functional programming has its own advantages and disadvantages. It can avoid a lot of bugs because functions can’t have unwanted side effects. They just take inputs, return outputs and leave the rest of the world (including their inputs) unmodified. On the other hand, that makes other use cases where you actually want to have some state that changes over time more difficult.

    Hope that helps.

    Edit: some cleanup and more precise explanations.

    Edit: replaced “paradigm” with “way of programming” to make @palordrolap@fedia.io happy.

    • dfyx@lemmy.helios42.de
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      5 months ago

      And another example that shows off pattern matching:

      Procedural:

      int minimumElement(int[] array)
      {
          if(array.size() == 0)
          {
              return 0;
          }
      
          int minimum = array[1];
          for(int i = 1; i < array.size(); i++)
          {
              if(array[i] < minimum)
              {
                  minimum = array[i];
              }
          }
          return minimum;
      }
      

      Functional (still, no specific language, just an example of what it might look like):

      int min(int a, int b) => a < b ? a : b;
      int minimumElement(int[] array) =>
          array match {
              [firstElement, ...rest] => min(firstElement, getMinimumElement(rest));
              [singleElement] =>singleElement;
              [] => 0;
          }
      
      • ieatpwns@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        5 months ago

        To a layman like me it looks like procedural looks more like logical coding and the functional version looks more like a math problem