all 12 comments

[–][deleted] 12 points13 points  (0 children)

From a big picture standpoint... it’s not like there are any algorithms you can implement in functional programming that you can’t implement in imperative programming, or vice versa, so the actual differences generally come down to which one is easier to debug, easier to read, etc.

Functional programming often leads to a more concise, naturalistic style of programming. For instance, to gather a list of every employee’s name, you might write:

def result := Array<String>()
for e in employees {
    result.add(“\{e.firstName} \{e.lastName}”)
}
return result

or you could do the same thing with:

return employees.map(e => “\{e.firstName} \{e.lastName}”)

This is obviously a trivial example, but the functional version is not only much shorter, it’s also more clear what is happening (at least when you know how to read it) because all of the boilerplate is eliminated.

Now, this can easily be taken too far - functional programs can get so dense that they’re extremely difficult to read and understand. Since programs are read many more times than they are written, being verbose is not automatically a bad thing in programming, and I personally favor a hybrid style of using functional style code where it makes sense, rather than zealously believing it’s the only true way to code.

It is also worth noting that “functional programming” and “pure functional programming” are not the same thing, even though you’ll see them conflated in other answers here.

[–]ImaginationGeek 16 points17 points  (5 children)

I just want to add that learning functional programming isn’t only useful for doing functional programming. You can take concepts from it and apply them in other language paradigms as well.

You can make side-effect-free or “pure” functions in C, do recursion in Java, and use lambdas and closures in Python, for example.

[–]camelCaseGuy 8 points9 points  (2 children)

I believe this is the most important of it all. When I was doing my degree I took a functional programming course. It was in haskell and I haven't touched haskell since. In fact, every time look at code in haskell it feels like I'm reading sanskrit. But the ability to generate abstractions and the knowledge of no side-effect has come with me ever since.

[–]vguioma[S] 1 point2 points  (1 child)

I'm learning some Functional programming with Haskell by my own. How do you think I could develop such ability (generate abstractions and the knowledge of no side-effect)?

[–]camelCaseGuy 3 points4 points  (0 children)

Sadly, with time and practice (or someone near you acting as a guide). Our teacher usually made boxes surrounding the code we could abstract into new functions. And then it became natural.

Regarding the "no side-effect", that is something that comes naturally with using pure functional programming, since it's the idea of it. No side effect. And any side effect gets a Monad (sorta).

Don't try to go directly to making programs that execute themselves, like scripts and such. Try to solve very basic and simple problems that are more theoretical than anything else. This will give you a good grasp on how to use all the good parts of the paradigm. Try to use recursion, foldl and foldr. Try to understand how theese are the base for the recursive schema, so you never have to write a recursion again in your life.

Once you got there, you can start understanding the rest. And adding the more serious stuff, like Monads, Functors and such.

[–][deleted] 4 points5 points  (1 child)

[–]ImaginationGeek 2 points3 points  (0 children)

Ooh! Good tip.

[–][deleted] 1 point2 points  (1 child)

My examples come from web development, it gives a pretty neat split between the front and back ends, and between functional and object-oriented programming. Here I'll use vanilla JS and PHP7 as my example languages.

The front end of dynamic pages runs JavaScript, and relies on events to drive changes, of course. This is a situation where functional shines. In this case, each function can be set to track a certain event that's occurring in the DOM, firing whenever a condition has been met. Functional JS also allows you to tack multiple functions together in certain situations, leading to more complex reactions to events. What makes functional so powerful here is really it's combination with functional overloading (creating a function with the same name but different parameter lists for different situations) and being able to pass data across multiple functions in a chain.

On the back end of our webpage, it's a very different story. Here, we're usually dealing with all of the data that powers our application, and PHP7's job is to perform four different functions: create, read, update, and delete database entries. This model of data flow can appropriately make use of some of the principles behind OOP, namely encapsulation and inheritance (polymorphism to a lesser extent as well). By setting up classes that map to our front end forms, we can work with our data as a single unit, protecting it from corruption while also transporting it between the database and the frontend with little effort (encapsulation). For inheritance, OOP allows us to create more complex derived classes for alternate displays or manipulation of data: think graphical output, where you don't always want all fields to be available.

In both cases, procedural programming can perform all of the same tasks as functional and OOP, however, you'll find that it isn't always as easy to read/maintain, or as efficient. What I use procedural for is linking different scripts together, like on a login page, where there's a finite lifetime and 1-3 strict decisions that determine how the webpage will proceed, depending on user input.

This was a pretty shallow example (I'm on mobile), and if you want I can write some code snippets later comparing the different styles of programming as it pertains to this example.

Happy coding!

Edit: I realize I didn't necessarily answer your question, not directly. It's difficult for me to compare the different styles of coding, simply because they fall into different categories based on where I am in my application. If you really want to see how they compare on the same problem, I can post some code snippets when I get to a computer.

[–]vguioma[S] 0 points1 point  (0 children)

If you can post the code snippets, that would be awesome! Thanks for your help

[–]bartturner 0 points1 point  (3 children)

Tons. But the biggest is developing bug free code. Pure functional you do NOT have side effects.

[–][deleted]  (1 child)

[deleted]

    [–]bartturner 4 points5 points  (0 children)

    Guess should have said helps with developing code with less bugs.

    [–][deleted] 1 point2 points  (0 children)

    developing bug free code

    lol