68
What are and what are the main differences between Functional Programming and Object-Oriented Programming?
68
What are and what are the main differences between Functional Programming and Object-Oriented Programming?
56
In theory, comparing these two paradigms makes no sense: the functional programming, to imperative programming and the logic programming (among others) refer to the way a given computation is described - respectively, in the form of mathematical functions (lambda calculus), in the form of explicit instructions to the machine (von Neumann architecture), and in the form of logical predicates to be proved by induction (resolution in first-order logic with setback). Already the object-oriented programming has more to do with how the data structures of a program are combined/united with the subroutines that have dealt with these structures (i.e. the data and behavior are combined in the same logical structure).
That is, one can have a language that is both functional and object-oriented - in the same way that one can have one that does not follow either paradigm.
In practice, most object-oriented languages follow the imperative paradigm, and similarly many functional languages (such as Lisp) include nonfunctional characteristics - such as state variables. For this reason, in the rest of the answer I will consider object orientation in an imperative context - although in principle it is possible to apply it to other contexts.
Functional programming is based on the mathematical concept of function, where for each element of its domain (input) set there is only one element in its counter-domain (output) set. In addition, functions are usually expressed by means of other functions - so obtaining the value of the function for a given set of parameters involves not only applying the rules of that function, but also making use of other functions.
For this reason, functional programming does not have the concept of "state variables"; if any value (be it numerical, textual, a vector, matrix, or something more complex) appears at any time in computation, that value is considered to be unique and immutable throughout the process. One can establish relations between values - how to say that 2
is half of 4
- but the value 2
will always be a 2
and the 4
will always be a 4
.
In imperative programming it is considered that data structures (again, numbers, text, vectors, matrices, etc.) exist in a given state, and that through specific commands to the computer this state is being changed in the course of the program, so that at the end of the execution it represents the consequences of a logical process. The available commands are fixed by language, and usually there are separate commands to handle types of data distinguished.
The concept of "object" is an extension of this behavior beyond the data types of the language itself, applying them also to the types defined by the user: for each data structure defined, a series of operations is also defined that can be applied to this structure - in general influenced by its internal state - and from then on it is allowed to use these structures as distinct types, and not as generic data structures. Add to this the concept of "more generic types" and "more specific types" (i.e. inheritance), and we have the possibility of creating a rich taxonomy of types each with its operations - so that the programs can be expressed in a higher level of abstraction than those using only the types provided by the programming language itself.
When objects and their relations are the central point of a proramation language, it is said that it is "object-oriented" (or, in the popular, object-oriented to objects) - because the entire logical flow of the program is based on what objects exist and how they interact with each other. That is, initially these objects are there - in their initial state - and as the program interacts with its environment (either through an input, or through user interaction) these objects will change their state.
Just as a function has domain and counter-domain, an object has operations that are only applicable to it in particular.
Just as a set can have subsets, an object can have subtypes; and just as a subset element can be used as a parameter for a function that has the main set as a domain, an object of the subtype inherits all operations of its base type (i.e. they apply equally and he).
There is [necessarily] no entry or exit point in a functional program: in principle, any of its functions could be called from an interactive input, for example. Similarly, an object-oriented system does not establish a "start" or an "end" for the program: each "message" sent to an object will cause a series of effects (and perhaps a response), but there is no predefined order for which messages will be sent.
The main difference is the question of the state: a function by definition does not have side effects, it only returns a result from a given set of parameters. Consequently, two or more calls from the same function passing the same parameters should produce equal results - while in the POO the result may vary depending on the state of the object at the time the Mensagen was sent.
As a consequence, while in the POO an object can be ignorant of the other objects that make up the system (it is enough to know its nearest neighbors and send messages to them) - reducing the coupling - this is more difficult to achieve in functional programming (and by my experience, also in logic) because of the immutability of data structures.
A
has a reference to the B
, and the internal state of B
changes, no need to notify A
because the subsequent messages they exchange will be influenced by the change of state (in POO). Already in the functional paradigm, A
and B
are the results of a computation - valid now and forever; if another computation produced a B'
, then it is necessary to produce also a A'
so that the desired relationship between the B
"new" and the A
fits your new configuration.On the other hand, this independence from a state makes it much simpler to evaluate and debug functional programs - because they can be tested part by part and the result of the whole will be identical to the sum of the parts. Whereas in the POO it is sometimes difficult to reproduce - or even discover - the state in which a set of objects was when unexpected behavior occurred, so as to inspect their behavior.
The function concept in functional programming is different than is used in the rest of mathematics. Functional languages in general contain partial functions: for a given X
, or f(X)
is a counter-domain value or the function does not return (due to an infinite loop, exception, etc.), which does not happen with functions of set theory.
@Darque I imagine you refer to the fact that these languages do not verify that the parameter belongs strictly to the function domain, right? Ex.: in f(x) = 1/x
the domain is ℕ*
, nay ℕ
; but if the function has been set to receive a int
, she could at first receive a 0
(causing an exception).
42
The mgibsonbr response is excellent and I could not have done better. I’ll just put something a little different:
Functional | OOP |
---|---|
Based on mathematical functions | Based on forms and behaviors of objects through fields and methods |
Lambda Calculus | Based on forms and behaviors of objects through fields and methods |
Principle of Compositionality | Heritage and Abstraction |
Recursion | Polymorphism |
Referential transparency | Encapsulation |
Computational purity (no side effects) | Side effects |
Lazy Evaluation (delayed execution) | Eager Evaluation (immediate execution) |
Immutability | Data is mutable |
Lineage based on "macros" (Lisp) or "Pattern matching" (ML) | Lineage based on "classes" (C++) or "prototypes" (Javascript) |
Things defined with many behaviors | Many things and defined behaviors |
Formulas/actions first | Data/nouns first |
Find the correction of the code | Search for code reuse |
Enforcement order does not matter (parallelism) | Order of execution is important |
Data loosely coupled to functions | Strongly coupled data and functions |
Concision (opinion?) | Explicitude (opinion?) |
Lisp, Scheme, ML, Scala, F#, Haskell, Erlang | Smalltalk, C++, Java, Python, Ruby, JS |
Of course there are some points that help understand the differences between them. I won’t go into detail about each point, it would give a book. The subject is a little more complex than that and there are some inaccuracies to simplify the exposure. The table should not be interpreted as a definitive comparison of differences. Neither is it a "either you choose one or the other aspect of each side of the table".
One of the most important points is that pure languages in one paradigm are usually less useful in most real world applications and do not achieve great success. For technical or cultural reasons, languages closer to the imperative paradigm are more popular.
Actually in OOP, the only popular languages are essentially imperative with object orientation characteristics and implement various functional programming concepts. And the main concept of the paradigm, which is inheritance, is often discouraged. Some people put another concept as the main one, but other concepts are not even unique to object orientation.
Functional languages that do not allow any kind of state change, or side effects (even if controlled) have extremely impaired utility for most of the problems.
Don’t be fooled when they try to sell paradigm purity. That’s great in academia. No paradigm is a panacea for anything.
Both paradigms provide abstractions, therefore inefficiencies will be present, each with its own characteristic. To reduce inefficiencies the purity of paradigm must be left aside; and the closer they approach the imperative, the closer they are to concrete processing.
Complementing according to the comments below, the concepts are orthogonal, and if it is not clear, in "real world" languages they are either functional or imperative with object-oriented characteristics.
Object orientation It is such a complicated theme that no one has a clear definition of what it is. For every person you ask it will give its definition. So if someone says you’re wrong, you probably are, but not the way they’re saying :) And Alan Kay, creator of the term, admits more recently that Smalltalk, his language, is actually message-oriented and not objects (see another controversy).
And it’s sad that a lot of people think that they’re object-oriented programming and they’re not, it’s like writing a book having a hard time in communication and expression classes. They go so far as to discuss which language is more object-oriented than the other, Java or C# (among others). Both are very little object oriented (Aaahhhh, marketing), and it is recommended to use little of the characteristics of this paradigm. This is because each one thinks that a certain characteristic is more important than another and none implements all the characteristics of the paradigm purely. Thank goodness!
And functional languages are far from controversial? No, many people consider that Lisp, which was the first functional language, is not purely functional and only those based on ML are. Moreover, some say that Lisp is one of the most object-oriented languages that exist (the very creator of the term).
Some say that orientation is even paradigm, and this is just another reason to show that they are orthogonal.
Today new languages and those that are wanting to be updated increasingly adhere to the functional paradigm. Of course, not purely, just pragmatically. I put in the Scala and F# in the functional table because they sell like this, but they are very imperative too, and they implement everything of importance of object orientation. So even imperative and functional can be used together, although there is a paradigm that is strong and the other only helps. C# is an example of an imperative language that is more and more functional, but will never cease to be predominantly imperative, and will maintain its object orientation (but that has less and less important role, since the 2.0 this has been falling).
In code organization, in its data structure that has more to do with OOP, the imperative procedural/modular and even the functional is more useful than the pure object oriented. Many do not realize this and continue to do the same always following the cake recipe you learned. People use OOP as a "good practice" (search here for what I think of it). Good practice is knowing what you’re doing and adopting what’s best for each case. Composition is the secret to good maintenance and today who does not stray from OOP creates objects that have nothing to do with the initial idea of the paradigm and it is common to modularize so much that borders on functional, modular and even procedural, but as it is within a class, they think that.
I explained some facts and gave some opinions, not least because this is a subject not so well defined and understood by all.
I think this table doesn’t make much sense - Functional Programming and OOP are orthogonal concepts, and can coexist. It makes no sense to compare one versus the other. Scala, for example, promotes Functional Programming and Object-oriented' 100% - in such a way, that it replaces static methods/classes with Companion Objects. F# is also object-oriented. I believe the comparison should be Imperative Programming vs Functional Programming.
@dcastro Then read the entire text, you repeated what I said in other words. And what I did not say, I made reference to the reply that mgibsonbr with which I agree 100%. If I made a different comparison, I wouldn’t be answering what was asked. Then I add to make it more explicit. I understood what you said, I’m not questioning it, but if it’s to be absolutely accurate, Scala or F# isn’t 100% OOP, they can be as much as Java, C#, C++ or other popular OOP languages, and none is 100% OOP.
I think you’re not saying that 100% of the OOP language, after all you said yourself that these languages are also functional (and complement that are also at least imperative, there are other paradigms), even if it was 1% functional (and it’s much more) the arithmetic no longer battery. If you say she implements 100% of the OOP concepts, then, well, Alan Kay disagrees with you. Anyway you can argue differently if I didn’t understand something. I would just like to emphasize, that I understand what you said and agree with everything. I’m just saying that being 100% accurate does not help understanding
I don’t know about F#, but Scala is 100% object oriented, in the sense that all values are objects, and all operations are called object methods. C# is not 100%, because the statistical methods/operators do not belong to any object. The same happens in Java, where there are primitive types, which are also not objects (as far as I know). I only meant that the two concepts are not opposite, are not black and white, so I found that a comparison table does not make much sense. I agreed with the rest of the text, but I think this point is' central and it was not clear.
Put another way: It is possible to write a program using only objects, and written completely in a functional style. The comparison table suggests that I have to opt for one of the two.
Bold: "popular languages are essentially imperative with object-oriented characteristics and implement various concepts of functional programming.". In addition, object oriented and using objects do not seem to me synonymous, so I believe that the table has no problem as a way to distort the perceptible differences in the concepts.
@Exact Bacco, the idea of the table is to make the concepts accessible to people. It is not a thesis. It may even have some inaccuracies and is certainly not complete. If there is information that can help improve it, I am listening to everyone. And I talked about the simplification made, I said that the paradigms complement each other. But I will clarify even more.
@Maniero, I, like the vast majority of programmers (imagine), have zero experience with functional languages. So when you say "C# is an example of an imperative language that is increasingly functional", what aspects of language are those that you say are functional?
@Pedrogaspar It is complicated to list all this in comments.
-5
Object orientation aims to mirror the real world. For example: a car, we can insert it into the system, which would turn it into an object. This object has attributes (such as color, make, power, etc.) that distinguish it from other objects. It is a discipline and type of programming that I like and prefer, but I made a summary because the subject is very extensive. I hope my reply helped(a).
Browser other questions tagged oop terminology functional-programming paradigms
You are not signed in. Login or sign up in order to post.
@Carloscinelli I tried to answer as succinctly as I could, but I also felt that in the way she is she is very wide. One way to improve it would be to put parameters that guide the answers (instead of leaving according to the interpretation of each one). For if several good answers arise - each addressing the question from a different angle - how are you going to choose one as "right"? And this - the possibility that is to accept an answer - is what differentiates a "question" from an open discussion (which, even if fruitful, does not fit the format of the site).
– mgibsonbr
I agree with @mgibsonbr (and his response won me in the first sentence). What interests you most in the question, functional programming or orientation to (ok, for) objects? Are not two concepts directly comparable.
– bfavaretto
@mgibsonbr do not know if the problem here would be to have several right answers and do not know which one to choose! For example, a very simple and straightforward question like this ( http://answall.com/questions/7527/como-consolidar-agregar-ou-agrupar-os-values-em--base-data) had 4 correct answers and choosing one of them was arbitrary, as none is intrinsically better! I agree that the question is a little open, but I think that although the question can be seen from different angles, there is a notion of completeness to judge one answer as more complete and the others as complementary!
– Carlos Cinelli