A comparison between functional programming and object-oriented programming

Source: Internet
Author: User

As a kind of structured programming, functional programming is being paid more and more attention. The project is not just object-oriented programming, but more people are trying to start using functional programming to solve problems in software engineering.

What is functional programming? In Wikipedia, a detailed definition, 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 states and variable objects. The most important foundation of a functional programming language is the lambda calculus (calculus). And the function of the lambda calculus can accept functions as inputs (arguments) and outputs (outgoing values).

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

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

Advantages of object-oriented programming

Object-oriented programming can be thought of as a kind of idea which contains various independent and mutually invoking objects in the program, which is opposite to the traditional thought. The traditional programming idea considers the program as a set of functions, or a series of instructions to the computer directly. Each object in object-oriented programming should be able to accept data, process data, and communicate it to other objects, so they can all be thought of as a small "machine", or object. It has been proved that object-oriented program design promotes the flexibility and maintainability of the program, and is widely used in large-scale project designs. In addition, supporters claim that object-oriented programming is easier to learn than ever before, 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, polymorphism characteristics, natural design of high cohesion, low-coupling system structure, making the system more flexible, easier to expand, and low cost.

The 23 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.

Create five types of models: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.

Structure mode, a total of seven kinds: Adapter mode, adorner mode, proxy mode, appearance mode, bridging mode, combined mode, enjoy the meta-mode.

There are 11 types of behavioral Patterns: Strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.

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 lead to uncertainty about data modification.

In today's software engineering, due to the abuse of object-oriented programming, many problems are caused. The first is to write reusable code and generate a lot of useless code, resulting in code bloat, and many people do not fully understand the object-oriented thinking, in order to object-oriented, so that the final code is obscure, to the maintenance of the later brought great problems. Therefore, for the development of large projects, the use of object-oriented will be some of the situation is not suitable.

Object-oriented Although the development efficiency is high but the code is running much less efficiently than the process, it also limits the object-oriented usage scenarios to those where performance requirements are critical.

Advantages of functional programming

In functional programming, because the data are all immutable, so there is no concurrent programming problem, is multithreaded security. Can effectively reduce the side effects of the program operation, for the fast iteration of the project, functional programming can realize the hot swap between functions and functions without worrying about the problem of data, because it is a function as the smallest unit, as long as the relationship between function and function correctly can guarantee the correctness of the results.

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

Disadvantages of functional programming

Since all of the data is immutable, all variables persist throughout the program's run and are very resource-intensive. At the same time, due to functional congenital design, performance has not been enough. While modern functional programming languages use a number of techniques, such as lazy computing, to optimize the speed of operations, it is not always possible to target object-oriented programs faster than object-oriented programs.

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

Summarize

Functional programming and object-oriented programming have pros and cons, a more liberal syntax, and a better robustness. As programmers should have a good understanding of both programming, either way, as long as the best solution to the current problem is the right way, after all, for the software engineering to solve the problem is the most important, the use of tools is not so significant, as for programmers, the language is not important, it is important to solve the problem of the idea.

Now the trend of the two is to learn from each other, many of the object-oriented language, such as Java, are added to the new version of functional programming support, and functional programming for reference to some in the object-oriented language with some of the compilation techniques to make the program run faster.

A comparison between functional programming and object-oriented programming

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.