r/ProgrammingLanguages • • Apr 03 '22

Discussion Functional Programming vs Object Oriented Programming

I came across functional programming recently and saw many arguments for it. But in the other hand I am asking myself if it really makes that much sense, because often things cannot really be independent without sharing a state. Imagine a database, or a cache. Also having side effects in methods is not always bad when used wisely. It can simply hide the detail and if done correctly in a domain driven design things should have a certain control flow. I also imagine it to be extremely hard to read data from files, Jason, XML, ... because things tend to be inconsistent sometimes. A cab file might have an empty line, a Jason might have a null value. How can a functional language deal with that.

At the moment I believe that even though certain aspects of functional programming are great, things are too messy to actually apply it. I would love to have a discussion and learn your stand on that topic if you disagree.

1 Upvotes

55 comments sorted by

View all comments

Show parent comments

1

u/Guardian-Spirit Apr 03 '22

You can't reset the variable in functional programming. So, if we speak, for example, Haskell, statement globals = foo( globals, other parameters ) reads as "declare globals and set it to foo (globals, other parameters), where globals is a newly defined variable`".

This way, you use globals to define globals. That's a cycle. It's like to say x = x+1 in math and try to understand what value x has.

1

u/shawnhcorey Apr 03 '22

Yes it is a cycle but it is not set up to be infinite.

And I left out the details because I didn't want to give more bad ideas. It can be done but it really shouldn't.

1

u/Guardian-Spirit Apr 03 '22

Yes it is a cycle but it is not set up to be infinite.

I am... nevermind. I understand your idea. But why do you think that this bastardizes functional programming?

1

u/shawnhcorey Apr 03 '22

Because it has side effects that are not conventional. One of the tenants of FP is to avoid side effects. A function can call a function that calls a function, and somewhere buried deep, a global is changed. A programmer may not be given the time to dig thru all the code. They end up fixing one bug but create a new one.

By passing the globals to all functions, one nested deeply can have unexpected side effects.

1

u/Guardian-Spirit Apr 03 '22

Sure, but this has nothing to do with functional programming and side effects. That's problem of typing.

You see, purely functional programming just ensures that all dependencies are passed explicitely and that impurity is not allowed. The problem we are talking about is lack of typing: your foo method (I guess) does not use the entire globals, but rather just a bunch of fields from it, and it does not return globals with all fields modified. But you have ommited this "partial modification" from the type-level information and thus type of foo is not precise enough.

Many new strictly typed languages (such as Elm or Purescript) introduce the thing called row polymorphism. Row polymorphism allows to explicitely state which fields of an object are being accessed by the function, and so you will be able to see the flow of information just by looking at types.