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.

  • ℕ𝕖𝕞𝕠@slrpnk.net
    link
    fedilink
    arrow-up
    1
    ·
    2 months ago

    It’s a programming paradigm where every action is made up of functions, functions which have no side effects beyond their explicit outputs.

  • c10l@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    2 months ago

    As others mentioned, it’s a programming paradigm. It and discussions around it have zero implications outside of programming.

    People can write great applications using it or using any other paradigm. Same is true for terrible applications.

    Some people love it, some people hate it, most are somewhere in between and think it has their merits and tradeoffs, and that it can be used where it makes sense, but shouldn’t where it doesn’t.

    Heated discussions are very common in tech circles over things that have zero practical implications outside our own little world, and this is one of them. 😄

    • surewhynotlem@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      2 months ago

      I get what you’re saying, but if your application doesn’t have 14 layers of inheritance, it’s barely an application.

      #Java4Life

  • dukeofdummies@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 months ago

    Nah nah nah, it’s just a different paradigm. It’s like… it’s a different meta, but for programming.

    If I was to ELI5, I’d say it’s the difference between making a series of objects that interact with each other (OOP) and and creating a very large console with lots of buttons (functional)

    Some problems are much easier object oriented. If you have a video game, building a projectile class and adding different types of projectiles makes things suuuper easy to build. Arrows move slow and deal x damage, beams move super fast and deal y.

    Functional can be easier to troubleshoot, also easier to crank out novel things, easier to make secure, and even make simultaneous operations trivial. It has very different problems though. You need to put more effort into eliminating dependencies.

    The two do not play well together at all they’re like fire and water. Sometimes you need water to soak something and make it easier to work, sometimes you need heat to melt something and make it easier to work.

    Telecommunications like phones or military applications tend to the functional languages, data, video games, and a lot of the popular languages tend to OOP.

    The transition between the two is jarring, and infuriating, but a knowledge of both can really improve your design skills.

    • raver@lemmy.rimkus.it
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      2 months ago

      In my opinion kotlin tries to slowly close the gap between functional and OOP programming. I love the implicit null checks, extension functions and the various list functions😊

      • acockworkorange@mander.xyz
        link
        fedilink
        arrow-up
        1
        ·
        2 months ago

        Dart also does that (importing fictional concepts), and treating functions as first class citizens opens up a lot of interesting options.

  • dfyx@lemmy.helios42.de
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    2 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
        1
        ·
        2 months ago

        At some point, using actual five year old words just makes communication harder rather than easier. OP mentioned that they’re at least somewhat familiar with some programming languages so I assumed they’d figure it out from context. But fine, I’ll change it.

    • dfyx@lemmy.helios42.de
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      2 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
        ·
        2 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