Do static methods make Java a pseudo-functional language?

I was thinking about a post from Misko Hevery that static methods in Java are deadly. I do not want to discuss the issue of testability, but more about the concept of static methods. Why do people hate so much?

It is true that we do not have closures (but we have slightly inconvenient anonymous functions), lambdas and functions as objects of the first class. In a way, I think static methods can be used to simulate functions as first-class objects.

+6
java closures functional-programming
source share
6 answers

One of the characteristics of functional programming is the immutability of data. static means you don't need an object (instance) representing state, so this is not a bad start. However, you have class-level state, but you can make it final . Since (static) methods are not first-class functions at all, you will still need ugly constructs, such as anonymous classes, to approach a particular style of functional programming in Java.

FP is best done in a functional language because it has the necessary language support for things like higher order functions, immutability, referential transparency, etc.

However, this does not mean that you cannot program in a functional style in an imperative language such as Java. Other examples can be given. This is not because you are programming in Java that you are doing OOP. You can program global data and unstructured control flows ( goto ) in a structured language like C ++. I can do OOP in a functional language like Scheme. Etc.

Steve McConnell mentions the difference in language programming compared to language programming in Code Complete (also a very popular reference to SO).

So, in a word, if you say that “static methods mimic first-class functions”, I disagree.

If, however, and I think it was a more important point that you tried to overcome, you would say that "static methods can help in programming in a functional style in Java," I agree.

+2
source share

Static methods make testing difficult because they cannot be replaced; it's that simple.

How do static “simulate” methods function as objects of first class 1 ? They are probably worse than anything else on this front. You can "simulate" functions as first-class objects by creating interfaces with a single method, and indeed, Google Java Collections does just that in several places (for predicates, forecasts, etc.). This cannot be done using static methods - there is no way (other than reflection) to convey the concept of "when you want to apply a function, use this method.

No, I don’t see how static methods help here. They prevent the state from changing (since the only state available is the global state and any mutable state passed through the parameters), but they do not help the “functions as first-class objects” side.

C # has better support for this (with lambda expressions and delegates), but even this is not the case as usual. (For example, compare it with F #).


1 As in the case of Java 8, method references will allow you to convert methods into instances of the corresponding interfaces of one method, which will make all this more relevant. Back in 2009, it was far, although ...

+4
source share

Functional! = Function, and for the record, I will argue that the method! = Function ...

Java is a statically typed, object oriented language. Java also maintained relative purity in this way, but not where it is next to a functional language.

Although it is true that you can mimic the behavior of functional programming with imperative programming, you will never get the neat syntax you want for calculating lambda. In a sense, if a language does not support the correct lambda calculus, it is not a functional programming language.

C ++ has functions, but C ++ also has classes. Therefore, C ++ has two types of functions, member functions and functions. When you say a method, you mean a member function. Because the method is called on the instance of the object. But when you say a static method, you mean only a function (in the sense of C / C ++). This is just a dictionary to reference the elements of your code. And in Java, code cannot exist outside the class, the method implies that it belongs to a type of type ie

So far, none of what I said is related to functional programming, but I think you understand that you are mistaken.

I suggest you take a look at pure functional programming languages ​​such as Haskell or Erlang. Because functional programming languages, as a rule, also do not have closers.

Your statement that static methods can be used to simulate functions as first-class objects sounds very strange to me. This is more like a dynamic programming language than functional programming.

+3
source share

My biggest objection to static methods is that they are not polymorphic and that they are not used in an object oriented way, instead you have a class (not an object) to access them.

0
source share

If you use only static methods, then you program in a procedural, not object-oriented style.

However, the only context I can imagine where this would be good is during the first programming lessons before introducing object orientation.

0
source share

In Java, you cannot give a function as an argument to another function.

In a functional language, if you have a function

 def addOne(i) = i + 1 

you can pass this to another function, which, for example, will apply it to all elements of the list.

In Java with

 public static int addOne(int i) { return i + 1; } 

there is no way to do this.

0
source share

All Articles