What are arguments against object-oriented programming

Object-oriented vs. functional programming on the web

Many web applications are implemented in an object-oriented manner today, but functional programming is experiencing a revival. It is not uncommon for a dispute to break out in the programming paradigm: some swear by functional programming, others by object-oriented programming. The truth is probably in between. We show the differences and tell you which paradigm is best used when.

For a better understanding, we start with the presentation of object-oriented and functional programming.

object oriented programing

In object-oriented programming (OOP), the software architecture strives to represent reality. So-called classes encapsulate data and implement functions for data manipulation. Today it is considered by many to be state of the art, especially in connection with design patterns.

Many OO implementations maintain the principle of secrecy, which is also known as information hiding. In this programming paradigm, variables are encapsulated in objects. In accordance with the principle of secrecy, variables are usually accessed using getter methods and setter methods. The programming style is often similar to the imperative / procedural style.

The central property of object-oriented software is reusability. Class properties can be used in new implementations through inheritance. Changes to the original class are made by overloading and overwriting functions of the parent class. The addition of interfaces and additional methods in the child class is also possible within the scope of inheritance.

Example of a PHP class:

The data is encapsulated in the class. This delivers different results for both calls. The change is brought about by calling the function.

Functional programming

Functional programming is assigned to declarative programming. This programming paradigm focuses on functions, it is based on the lambda calculus. Functional programs are stateless and data (variables) cannot be changed. That includes advantages. The functional programming is well equipped for parallelization and is easy to test. It is even possible to verify the stateless functions.

The functional code is usually easy to understand. The functions are clear thanks to statelessness and unchangeable data. The design phase is also easy.

Stateless functions

Stateless functions have one central property: They always deliver the same result for an input. No internal condition can cause variances in the result. This is the key argument with regard to software tests and software verification. An example of these functions are anonymous functions that are available in Java, JavaScript, C ++ and, since version 5.3, also in PHP.

Example code for an anonymous (stateless) function in PHP:

The function can be called as often as required. The output depends on the input values, but is deterministic. The transferred objects are not modified by the anonymous function. This follows the principle of functional programming.

Another classic example of functional programming is the recursive calculation of the factorial:

When does which programming paradigm make sense?

There is no all-inclusive winner. Functional programming shows its strengths when testable and verifiable code is required. Functional programming is also well suited for code that can be easily parallelized. The examples also show that the mostly imperative object-oriented programming can easily be combined with the functional.

The recursive programming method of functional programming, as we see it in the third example, is dangerous: the data and the program code are loaded onto the stack for each recursion. Comprehensive operations that, for example, establish connections to other servers or databases and call up large amounts of data can quickly become memory hogs. Then there is a risk of memory overflows and program errors.

You can find more information and examples on the trend of functional programming in web applications under the following links:

http://angularjs.de/artikel/angularjs-baconjs
http://www.codediesel.com/php/anonymous-functions-in-php/
http://currybuch.de/
http://www.magjs.de/2012-01/strehl/strehl.html
http://drupal.cocomore.de/blog/drupalcon-review-funktionale-programmierung