A comparison between functional programming and object-oriented programming--programming ideas

Source: Internet
Author: User

As a kind of structured programming, functional programming is being paid more and more attention. Project is not just object-oriented programming, more people try to start using functional programming to solve problems encountered in software engineering.

What is functional programming. A detailed definition is given in Wikipedia, functional programming (English: Functional programming) or function programming, also known as functional programing, is a programming paradigm that treats computer operations as mathematical function calculations and avoids the use of program state and variable objects. The most important foundation of a functional programming language is the lambda calculus (lambda calculus). Furthermore, the functions of the lambda calculus can accept functions as input (arguments) and outputs (outgoing values).

In object-oriented programming, object-oriented Programming (English: object-oriented programming, abbreviation: OOP) is a kind of program programming paradigm with object concept, and it is also a method of program development. It may contain data, attributes, code, and methods. Object refers to an instance of a class. It encapsulates the object as the basic unit of the program, encapsulating the program and data to improve the reusability, flexibility, and extensibility of the software, and the programs within the object can access and frequently modify the data associated with the object. In object-oriented programming, computer programs are designed to be objects related to each other.

The relationship between objects and objects is a primary concern in object-oriented programming, in functional programming, all data is immutable, different functions exchange information through data flow, function as the first-class citizens in FP, enjoy the same status as the data, can be passed as parameters to the next function, It can also be used as a return value.

The advantages of object-oriented programming

Object-oriented programming can be thought of as an object that contains various independent and mutually invoked objects in a program, which is just the opposite of traditional thinking. Traditional programming advocates view a program as a set of functions, or directly a series of instructions to a computer. Each object in object-oriented programming should be able to accept data, process data, and communicate data to other objects, so they can all be considered as a small "machine", or object. It has been proved that object-oriented programming has extended the flexibility and maintainability of programs and is widely used in large project design. In addition, supporters claim that object-oriented programming is easier to learn than previous practices because it allows people to design and maintain programs more easily, making them easier to analyze, design, and understand. At the same time it is also easy to expand, due to inheritance, encapsulation, polymorphic characteristics, natural design of high cohesion, low coupling system structure, making the system more flexible, easier to expand, and lower cost.

23 Kinds of design patterns developed on the basis of object-oriented programming are widely used in today's software engineering, which greatly facilitates the writing and maintenance of code.

The creation pattern, altogether five kinds: The factory method pattern, the abstract factory pattern, the single case pattern, the builder pattern, the prototype pattern.

Structure mode, a total of seven kinds: Adapter mode, adorner mode, agent mode, appearance mode, bridging mode, combination mode, enjoy meta mode.

There are 11 types of behavioral Patterns: Policy mode, template method pattern, observer mode, iterative sub mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.

The disadvantages of object-oriented programming

Object-oriented programming takes data as its core, so in multithreaded concurrent programming, multiple threads can manipulate data at the same time, which may result in uncertainty of data modification.

In today's software engineering, due to the misuse of object-oriented programming, many problems have been caused. The first is to write reusable code to create a lot of useless code, resulting in code expansion, while many people do not fully understand the object-oriented thinking, in order to object-oriented object-oriented, so that the final code obscure, to the later maintenance brought a lot of problems. So for the development of large projects, the use of object-oriented will appear some of the situation is not appropriate.

Object-oriented Although the development of high efficiency but the efficiency of the code is much lower than the process-oriented, which also limits the object-oriented use of the scenario can not include those who are very demanding of performance.

The advantages of functional programming

In functional programming, because the data are all immutable, so there is no concurrent programming problems, is multi-threaded security. Can effectively reduce the side effects of program operation, for fast iterative projects, functional programming can realize the hot swap between functions and functions without worrying about the data, because it is the smallest unit of function, so long as the relationship between function and function is correct, the correctness of the result is guaranteed.

The expression of functional programming is more consistent with the grammar of human daily life, and the code is more readable. The code needed to implement the same functional programming is much less than object-oriented programming, and the code is more concise and clear. Functional programming is widely used in scientific research, because in the research of the code for the engineering requirements are relatively low, more simple to write, so the use of functional programming to develop faster than the use of object-oriented is much higher, It is more efficient to use a function in scenarios where the development speed is high but the requirements for running resources are low and the speed requirements are low.

Disadvantages of functional programming

Since all the data is immutable, all variables are present throughout the program's operation, and are very much in use for running resources. At the same time, the function of congenital design leads to insufficient performance. Although modern functional programming languages use a lot of techniques such as lazy computing to optimize the speed of running, it is not always possible to compare object-oriented programs with the speed of object-oriented programs.

Functional programming has been in existence for many years, but there are still a lot of problems to be solved in engineering to use functional programming on a large scale, especially for larger projects. If the understanding of functional programming is not deep enough, it will lead to the same obscure situation as the object.

Summarize

There are pros and cons to functional programming and object-oriented programming, a more free syntax, and a better robustness. As a programmer, you should have an understanding of both of the programming methods, no matter which way, as long as can solve the current problem is the right way, after all, for software engineering to solve the problem is the most important, with the tool is not so significant, as for programmers, language is not important, it is important to solve the problem of the idea.

Now the development trend of the two is to learn from each other, many object-oriented language, such as Java, and so on in the new version of the addition of the support of functional programming, and functional programming has borrowed some of the object-oriented language in the compilation techniques used to make the program run faster.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.