So what? How is that a problem?
As an example, suppose I have a static method that takes in an object and returns a result based on the state of that object. Now I have conditional logic in the unit under test that has a dependency on the result of that static method. In my unit test, I don't want to go to the trouble of creating the state of the object to force the delegate call to return a certain result that determines the execution flow, I want to just force that call to return the result I need.
Your method should follow a well rested interface for any touches that are outside of it:
* The input
* The return
* The side effects
You should be able to test that you've fixed a bug where you're calling a dependency 100 times instead of 1 time
It certainly sounds like your stateful object is too complex. Simplify your state.
> I write functions using ‘static’ all the time. I prefer that a function be static.
This, however, does not mean that all the functions should be static. IMHO, the more static stuff you can extract from your instance methods while keeping parameters count to max 3 (magic number that works for me), the better.
Yes this is the way. [1]
[1] https://en.wikipedia.org/wiki/Hexagonal_architecture_(softwa...
Small nitpick :p
Although due to the ambiguity of the word "function", your nitpick still has its place.
I would really like a way to express pure functions in Java. There would need to some way to mark objects as immutable at the language level.
It annoys me even more than no-one seems to care about it much, and most think final is good enough - you can still mutate final objects. In C++ all references are final (in a Java sense) since they can't be reseated, but const actually gets you immutability (modulo const_cast).
I just want everything to be immutable unless mutability is really truly needed!
Otherwise Scala is pretty pragmatic about it (val vs var).
In both cases you still get only shallow immutability, but if nigh every object itself is immutable, shallow immutability may not be so shallow.
Have you tried Rust? It’s nice in this regard; variables are immutable unless explicitly declared mutable.
But it's not terribly easy or even a good idea to switch from Java to Rust at a real company that has no time to rewrite libraries, has to hire developers, and so on.
In C++ you can write a class with some methods that mutate, and some const methods that can't mutate anything, and you can pass around const references through which mutation is guaranteed to be impossible. This all comes for free.
I was thinking that would make it possible to create unmodifiable data objects using that (and not Object as base class).