Question Driven – Essential differences between methods and functions?

Function is an abstraction of process, while method is an abstraction of behavior dependent on object. In the most commonly used data set transformation scenario, pure transformation process (function) unrelated to any object is more needed, such as map and filter. At this time, the previous “interface – anonymous object” method of Java8 appears to be extremely redundant and cumbersome. Thus limiting the development of the last API.

The above issues expose the limitations of the programming model underlying Java8’s previous language architecture, which led to functional programming in Java8. The warehouse address

Programming ideas/models that allow developers to organize or write Code describing logic that is ultimately processed by the appropriate compiler or interpreter into a computer-executable binary stream.

Programming model (thought) is regardless of language, but it is the lowest level of all kinds of computer language, language level, framework level and even the upper application level API syntax and thought support. Mainstream high-level languages have always tried to provide developers with multiple programming models that are easy to express in order to attract developers to commercial-grade projects in many ways.

Development oriented programming languages

The program itself is the abstraction and simulation of the real and developing physical world, that is, the program always receives and responds to various internal or external events or stimuli from the moment it is started, develops from one state to another, and finally terminates. It can be seen that the program is development-oriented and change-oriented, and the program language is how to better describe and organize these “developments”, and finally form a complete program body. Abstract from a philosophical point of view, development or change can be expressed in two ways:

  • Materialized expression

Development can be seen as driven by the interaction between things, such as when a ball is kicked from a static state to a rolling state.

  • Process description

Development can also be seen as the result of the superposition of one stage after another, a small process. For example, a factory product always goes through the stage transformation of multiple production lines and finally becomes a mature product.

These two expressions are different interpretations or abstractions of the same development, and there is no absolute good or wrong. High-level languages such as NodeJs provide developers with two ways to express development or logic, namely object oriented programming OOP and process oriented OPP programming. This gives developers the freedom to choose the best “presentation” for their scenario or needs, balancing consistency and efficiency.

Object oriented programming OOP

Object Oriented Programme is the further abstraction and refinement of “materialized expression”. The structured Object Object is taken as the first-class citizen, and the program is driven by many objects and their interaction (Method). The program itself is the outermost and the largest Object of the whole. The most typical language is Java.

  • advantages

    Structure, objectification is in line with people’s habits of thinking, conducive to the organization and classification of different parts of the program, plus OOP contains three characteristics – encapsulation, inheritance, polymorphism, nature is to solve the program redundancy and born, so OOP as a whole, macroscopically is very suitable for engineering, design friendly.

  • disadvantages

    Simple OOP is heavy, and it must rely on first-class citizen Object to define methods, which is not suitable for stage and microscopic processes or changes. This is also an important reason why Java has been criticized for being overweight, such as Map mapping and filter filtering, which have nothing to do with specific objects. It is just a little process itself, and therefore is better expressed in terms of process, OOP.

Process oriented programming OPP

Procedure Oriented Programme is the further abstraction and refinement of “process expression”. Functions are taken as the abstraction of phased and microscopic processes or changes. Procedures are accumulated by countless process functions, and finally achieve the development result of quantitative change but qualitative change.

  • advantages

Procedure functions are free, independent of any object, and therefore have a high degree of freedom, can be defined and combined (interact with) with other functions anywhere and at any granularity, making them ideal for phased, small, and object-independent processes, such as map transformation processes.

  • disadvantages

Free function is very loose, the process of “consciousness” of the highly demanding for the developer itself, otherwise the code readability and maintainability is difficult to guarantee, is very disadvantageous to the design of structured, typed, and not very good simulation objective physical world, so the overall is not conducive to engineering, the design is not friendly.

Evolution of Language

From the above simple contrast can be found that object-oriented OOP and process oriented OPP has its advantages and disadvantages, object-oriented, more suitable for macro frame, the design of the component class and encapsulation, and oriented to process was more suitable for micro, stage or the joining together of transition, the real “good” language should provide these two organizations or logical expression way, In fact, this is the current trend in the mainstream high-level languages.

  • The evolution of ancient languages

Due to historical reasons and development restrictions, the old GENERATION of C language such as Java, JavaScript, etc., only support a single programming idea, such as Java only support object-oriented OOP, Object is a first-class citizen, does not allow any free variables and methods, JavaScript only supports function-oriented OPP, and functions are first-class citizens. Although it can basically achieve the ability to “write” a complete and complex project, it also has the corresponding limitations of a single model, so Java gives people the feeling of being too old and not flexible enough to express details. JavaScript or Python, on the other hand, is too fragmented, redundant, and not objectified and structured.

Now these old GENERATION C languages are also gradually aware of this limitation, and the new version of the gradually provide a fitting each other’s scheme, such as Java with a single method interface and anonymous object simulation function to gradually make up for the shortcomings of procedural OPP. Both JavaScript and Python emulate OOP through prototype inheritance, and eventually add separate class keywords or syntactic sugar to gradually support OOP.

Java functional programming

The ES6 class class defines syntactic sugar

  • The language of the new generation

Golang, Kotlin and Scala, as the representatives of the NEW C series, have wisely chosen to support both object-oriented OOP and procedural OPP, giving developers a balance of flexibility and design, making the programming experience smoother.

Kotlin, for example, provides a process-oriented functional keyword func, meaning that functions can be defined anywhere and in detail

fun map (){
  println("This is map function")
}
Copy the code

Also provides object-oriented OOP keyword – class, which can realize the encapsulation of materialized components

abstract class Person(open val age: Int){ abstract fun work() } class MaNong(age: Int): Person(age){ override val age: Int get() = 0 override fun work() {println()}}Copy the code

Object oriented programming OOP

As the above analysis shows, the object-oriented programming model is structured, design-friendly and therefore the preferred choice for many emerging high-level languages.

In OOP, Object is undoubtedly the core, the first, the abstraction of objects and interactions (actions, such as kicks) in development. Therefore, objects are equivalent to the concept of entities in the real world. More abstractly, object is object, which is a collection of representation and behavior, that is, object/object = representation + behavior. For example, 🚗 a car is determined by its representation shell, tire, steering wheel, and its behavior of being able to run at high speed with refueling, which makes it a car 🚗 rather than a static 🌲.

The representation of the above object is further abstract, that is, the language’s exclusive noun – attribute/field, and the behavior of the object is another exclusive noun – method, the above statement into OOP language, that is

/** * object 🚗 = attribute/field + behavior */ public class Car {/** * Car */ private String body; /** * private String[] wheels; Public void runOnRoad() {system.out.print ("I am a Rolls-Royce, Go Away"); }}Copy the code

Such a template for describing an object, called a class, is usually defined by class and is common to all languages. So one of the most simple OOP model is marked out, OOP model is simple, of course, as a complex system, which has hundreds of thousands of classes and objects, the definition of just such a simple constraint is not enough, redundancy, chaos can occur easily, so the OOP containing natural * * 【 package 】, inheritance, polymorphism 】 【 】 * * the three features, It is used to guide developers on how to better design classes and objects to better use OOP models, which will be dissected in a later section.

Process oriented programming OPP

In OPP, the core and first-class object is undoubtedly the process Function, which is the abstraction of the stage and minor change/development process. The program is the largest function with countless process functions, combined and superimposed, with JavaScript as the most classic.

/* */ function settupWheels (car) {console.log('setup wheels for car', car) } function fillGas (car) { console.log('fill gas for car', car) } function start (car) { console.log('run to 200km/h', car) } var car = {} settupWheels(car) fillGas(car) start(car)Copy the code

With the development of functional number theory, functional programming has greatly expanded the flexibility and ease of use of traditional OPP, and has been gradually absorbed by various languages, such as Java and JavaScript functional programming.

Higher-order function-function interface

default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
    Objects.requireNonNull(after);
    return (T t) -> after.apply(apply(t));
}

/**
 * Returns a function that always returns its input argument.
 *
 * @param <T> the type of the input and output objects to the function
 * @return a function that always returns its input argument
 */
static <T> Function<T, T> identity() {
    return t -> t;
}
Copy the code

Conclusion: 1+1>2

It can be concluded from the above analysis that object-oriented OOP and process-oriented OPP have their distinct advantages and disadvantages, and they complement each other to jointly build engineering-friendly and smooth development experience language system, which is gradually becoming the mainstream trend of high-level languages. In the encapsulation of functional components, OOP is more conducive to structuralization and design, while for local and microscopic “stitching”, it is more suitable for flexible process functions, namely OPP.

For example, in Stream programming, Stream objects abstract and define a transformation pipeline, while microscopic transformation nodes are flexibly customized by procedure functions