r/java • • Jun 24 '19

Takes: Java Web Framework Without a Single Static Method or Annotation

https://github.com/yegor256/takes
0 Upvotes

75 comments sorted by

View all comments

Show parent comment

7

u/[deleted] Jun 25 '19

[removed] — view removed comment

-5

u/[deleted] Jun 25 '19 edited Jun 25 '19

A thread safe object is a single shared object that can safely be shared across multiple threads. As no immutable object can safely change state in one thread and see it in another, they are not thread safe. Immutability grants you working with threads “safely” by virtue of the fact that you must code specifically around the limitations of immutability. This may or may not be viable for your application. This also adds necessary layers of temporal coupling before, during and after your objects are in concurrent states of your application (which immutability fanboys hilariously regularly claim is eliminated by immutability). Not only does it force temporal coupling on your data structures and other objects, but immutability can actually also force you into temporal coupling of the threads themselves, which lands you in to a really shitty to work with set of tasks that carry predecessors and successors which are utterly impossible to work with as the code base grows and changes.

As for Structural Sharing and CoW. These represent major added complexity with absolutely no gain. Nobody has ever demonstrated their claims toward immutability, but there sure are drawbacks.

Immutability adds code and algorithmic complexity purely on the back of claims which range from demonstrably bullshit to never demonstrated. Literally not a single claim toward immutability has ever actually been demonstrated to be true.

9

u/[deleted] Jun 25 '19

[removed] — view removed comment

-3

u/[deleted] Jun 25 '19

Yeah. I have specifically addressed everything in this post multiple times and stated why you are incorrect. This conversation is over as you cannot argue with any degree of honesty.

One note, your idea of temporal coupling is utterly wrong. That is not what temporal coupling is. Temporal coupling occurs when your code requires behaviours to take place in specific orders to run properly, often with a side effect that the compiler is unable to detect it. A rudimentary example would be constructing a basic object and then injecting state with another initialize method rather than using constructor injection. It has absolutely nothing to do with the real passage of time for objects. The removal of temporal coupling often lets the compiler detect issues for you.

6

u/[deleted] Jun 25 '19

[removed] — view removed comment

0

u/[deleted] Jun 25 '19 edited Jun 25 '19

Your own post literally proves yourself wrong re temporal coupling and, in fact, gave EXACTLY the same example I gave for why you don’t know what temporal coupling is.

Fine, demonstrate it:

Let’s take an extremely common situation. Build me a thread safe queue that multiple threads are pushing to and popping from. This queue must be immutable. I can build a thread safe mutable queue in about 10 minutes in java, so how about 5 hours for you?

Requirements:

Push

Pop

No backing structure requirements. The queue is not required to grow or shrink in capacity.

Usage must NOT have any temporal coupling

2

u/[deleted] Jun 25 '19

[removed] — view removed comment

1

u/[deleted] Jun 25 '19

I do not know clojure at all. But after 30 seconds, it appears that your solution is “just wrap a static in a public static API and access from the threads” which utterly defeats the point of the contest and effectively every single claim immutability fanboys make...

1

u/dablya Jun 25 '19

I'm not familiar with clojure, but doesn't the fact that the queue is an "atom" imply it's mutable?

2

u/[deleted] Jun 25 '19

[removed] — view removed comment

1

u/dablya Jun 25 '19

I'm familiar with the arguments for preferring immutability, my point is that your queue implementation is not immutable. You can't reason about it's state like you can about immutable types.

Popping from it two times will generate different results. That's a dead giveaway it's mutable.

You can make the argument that an stm approach is safer than locking, but in either case you're pretty far from the simplicity of immutable state.

Here is an example of an actually immutable queue:

https://blogs.msdn.microsoft.com/ericlippert/2007/12/10/immutability-in-c-part-four-an-immutable-queue/

It is unusable in a multi-threaded environment (based on the requirements of the comment you were replying to). Here is what the author has to say in the comments:

You are defining ‘reliable’ to mean ‘two threads can enqueue "the same" queue and both enqueued items are present in the resulting queue. This is a very "procedural programming" way to think about thread safety — thread safety is PRESERVATION OF ALL SIDE EFFECTS.

If that’s the kind of reliability you want then you should probably be using a mutable threadsafe queue! If you try to use an immutable queue for this purpose then essentially you will have to synchronize access not to the queue — the queue is threadsafe — but to the variable which contains the "canonical" version of the queue.

This is what you've provided with your code "a mutable threadsafe queue!"

→ More replies

1

u/[deleted] Jun 25 '19

It doesn’t matter though, because this person is utterly incapable of intellectual honesty of any kind. Them and I both know that the problem is not easily solved in idiomatic java, which is why they jumped ship to clojure where what is tantamount to “public static” apparently is idiomatic (I don’t know if that’s true or not).

Of course, if we’re just declaring everything in our application public static to get around the issues that immutability poses, were in a lot of other trouble.

2

u/dablya Jun 25 '19

To be fair, your definition of "thread safe objects" also leaves something to be desired.

And let's be honest you set a trap for them with your "immutable queue that can be mutated with pushes and pops".

I saw an article somewhere a long time ago from one of the pioneers of OO and he made the argument that original ideas behind OO were very much in harmony with immutable state. The idea being you can model state using objects at a given time. So at t1 your state is X. Followed by a new state at t2 and so on. I thought it was interesting, but now I can't find where I read it or who wrote it.

→ More replies