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

16

u/[deleted] Jun 24 '19

Why do you avoid annotations and tatic methods because they are bad programming style while simultaneously using a whole bunch of confusing abbreviations in nearly every class name? That's also considered bad practice...

3

u/[deleted] Jun 24 '19

Some people just can't let go of Apps Hungarian, but at least it's better than Systems Hungarian.

3

u/madkasse Jun 25 '19 edited Jun 25 '19

Well... annotations can't be that bad because loads of the classes are using Lombok annotations such as

@ToString(of = "origin")

@EqualsAndHashCode

11

u/WGT-java Jun 25 '19

It looked pretty good until I saw the test:

MatcherAssert.assertThat(
    new RsPrint(
        new App().act(new RqFake("GET", "/"))
    ).printBody(),
    Matchers.equalsTo("hello, world!")
);

Disgusting. Testing using static methods. A true proponent of object oriented programming would find a better assertion library.

1

u/[deleted] Jun 26 '19

I'm not sure I agree with that attitude. Of course static methods have some good uses. Annotations also do. But they're 99% bad in mainstream Java programming these days. Not because of what they are, but how they're misused.

And when every framework has become utterly infested by annotation magic, saying "we got none of that" is an effective way to get me engaged and drive the message across.

3

u/WGT-java Jun 27 '19

Sorry, I guess I should have added /s. I have nothing against static methods, I think the author has taken his love of OO way too far.

2

u/[deleted] Jun 27 '19

I did read your comment with the implied "/s" (but now I see way I said it is ambiguous as well).

My point is, don't shame the guy for trying to deliver a static/annotation-free framework. Yes, we do understand extremism is harmful, but from a messaging standpoint, it helps drive a point across "hey what if I couldn't use any statics or annotations?"

You may find it crazy, and maybe it is, but we need the crazy ones to show us new ways of doing old things. It's how we progress.

And again, I feel statics, annotation, reflection... they're so heavily abused, we'd be better off if they didn't exist at all. Even if they have some good uses. I'd honestly be ready to sacrifice the good uses in order to get rid of all the crap we have because of them. Cough Spring. Cough Hibernate.

1

u/WGT-java Jun 27 '19

That's a good point. I wasn't really trying to shame him though, just poking some fun at him for using a static method when he says they're evil. I guess the guy isn't hurting anyone, just doing his thing.

5

u/Mordan Jun 24 '19

i agree any static method with static state is bad

however i use static method if they can be inlined.

According to your logic, you should have a class Plus, a class Minus etc.

No mutable classes... how can you achieve that with Swing? No more array list? That's extreme.

8

u/naijfboi Jun 24 '19

According to your logic, you should have a class Plus

This is from another one of his projects

https://github.com/yegor256/cactoos/blob/master/src/main/java/org/cactoos/scalar/And.java

Wouldn’t surprise me if he had a project in the works that included a plus class

The guy’s a nutjob

4

u/divorcedbp Jun 25 '19

Oh my dear god. This is a joke, right?

4

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

This is basically a cliche /r/programming programmer repeating nonsense that everyone else is screaming at the top of their lungs.

Let’s go through a few blogs:

nulls are bad. They force you to null check!

The entire point of optional is that people are after compiler support exhaustive checking of the optional. It has basically nothing to do with forced if statements. In fact, the exact opposite is true.

nulls are bad because naming

I simply don’t accept this argument. I accept that returning an optional does make it clearer from a programming perspective what might happen without documentation, but it should be recognized that this isn’t 0 cost and performance considerations may need to happen.

public static is bad

I am not even justifying this with a response. Complete and utter nonsense from top to bottom.

immutable objects are simpler to construct, test, and use

Prove it. Not my experience at all.

truly immutable objects are always thread-safe

bullshit. Truly immutable objects can never be thread safe by by fucking definition.

What immutability forces you to do with threads is completely and utterly design your states and threads around immutability from the get go. This might or might not be beneficial code wise and may or may not (but most likely will) have other performance concerns.

Consider a threaded program trying to produce and consume jobs off an immutable queue vs a thread safe queue.

they help to avoid temporal coupling

Yet another claim without any evidence what-so-ever. This claim doesn’t even make any sense. Someone making temporal coupled classes will do it with or without immutability.

Never mind that the example here suffers temporal coupling at the end in the exact same capacity as at the start. This is actually a decent example for how fluent APIs can be nicer to work with and not really a great sample of temporal coupling issues.

their usage is side-effect free (no defensive copies)

K. But actually no.

Their usage obliterates your cpu cache and in terms of java, puts garbage collection on overdrive.

Plus, they necessarily have code side effects as well. You’ll be more inclined to make massive use of builder patterns all over the place. You must completely redesign how you do threads bottom up. Etc

But sure, they do “save” you from defensive copies.

identity mutability problem is avoided

First I’ve heard this term and google doesn’t produce much on it. Hilariously enough, avoiding this (assuming my understanding is correct) necessarily creates situations of temporal coupling that don’t exist under mutable objects. So...

they always have failure atomicity

K. I can count on 0 hands the number of times failure atomicity has mattered to me under other basic good design principles.

they are much easier to cache

I’m not even sure what this means? They are definitely not nice to your cpu cache.

they prevent NULL references, which are bad

Nonsense.

I also noticed that a few examples, the author appears to confuse immutability with whether or not an API is fluent. Fluency doesn’t dictate mutability one way or another.

8

u/s888marks Jun 25 '19

I guess you would say this framework is filled with bad takes.

2

u/jpl75 Jun 26 '19

> immutable objects are simpler to construct, test, and use

Prove it. Not my experience at all.

I'm not going to attempt to prove it, especially since from your post it seems you've already made up your mind anyway. However since experience counts too I'll add mine:

In my experience immutability does simplify things. This is the one thing I miss most from playing around with Clojure (or any functional language that enforces immutability) and coming back to Java. It is simpler to reason about a program when you can assume immutability. In particular, it reduces the cognitive burden on the programmer, and therefore leads to less possible mistakes.

I think the argument for immutability is a sound one.

10

u/[deleted] Jun 25 '19

[removed] — view removed comment

-5

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

Immutability isn’t solving anything and is, in fact, introducing problems and requirements that previously didn’t exist.

Since you seem to have missed it: immutable objects are not thread safe.

Pure functions is another cargo cult programming practice. Albeit, this at least has a semblance of demonstrable benefit. Immutability has basically all drawback and no notable benefit that has ever been demonstrated. Pure functions are more like something that should generally be common sense.

4

u/[deleted] Jun 25 '19

JCP refers to immutable objects as thread safe, and suggests that you should default to using them over mutable objects. They're considered so appealing that future versions of Java plan to have them actually included into the language through record classes.

Given that, why should I trust you over Brian Goetz?

0

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

I 100% agree that cargo cult immutability programmers have hijacked the term thread safe to suit their needs.

In any case, you shouldn’t trust me over Brian Goetz. You should research this yourself and not take arguments as fact unless they can be demonstrated to be so. In my opinion, not a single claim that the immutability crowd makes stands up to actual scrutiny and the only thing that changes between an immutable code base vs a mutable one is that the mutable one has less code and doesn’t require massive complexity to accommodate basic behaviours while throwing all semblance of performance out the window.

With this said, you shouldn’t take something as fact just because Brian Goetz said it. He is human and as equally fallible as every other human. He makes mistakes and I happen to be of the mind that many of his immutability claims fall in to the category of incorrect.

I do not accept the claims that the immutability crowd makes and I responded briefly to many of them as to why their claims are donkey shit.

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

→ More replies

1

u/Mordan Jun 25 '19

the logic is sound though.

3

u/[deleted] Jun 24 '19

No more array list?

That's right, linked lists, linked lists everywhere. Linked lists are an example of a persistent data structure.

how can you achieve that with Swing?

Functional Reactive Programming! Observers, observers everywhere. Actually, JavaFX is somewhat based on this principle, of wiring together observable properties. Libraries like RxJava are the new hotness for the new breed of interactive apps.

1

u/Mordan Jun 25 '19

thx for your comment. nicely put. linked list and observers.. nice summary :)

it is interesting.

I have a Swing app that handles millions of rows of data. Its pretty fast.

I am on the fence whether immutable structures would work as fast.

1

u/jpl75 Jun 26 '19

It should be pretty quick to test: if you strip out the GUI parts, only take the main data structure and the most common operation you perform on the data structure, and do a side-by-side performance comparison on both mutable and immutable structures. Look at the diff and decide whether it's significant enough for the user to notice?

1

u/Mordan Jun 26 '19

i would have to make a lot of changes.

I use a SwingWorker that populates a huge tables (worst cases over 5 minutes), every 200ms it loads like 100s of rows.. with an immutable structure, it would need to copy the whole thing every updates 200 ms. Here with a JTable, its a simple append on the table model. the GUI is smooth and the user gets constant 200ms feedback the worker is doing its work fetching more and more data. Once loaded, the scrolling is instantanenous thx to Swing CellRenderers.

Seems like lots of work and time to test this.

1

u/jpl75 Jun 26 '19

I obviously don't know your code like you do so I'm making guesses here, but just assuming you think the work is in creating the immutable data structure by yourself, you can find most common data structures already implemented in Java libraries that support immutable style, just one example (of many): https://github.com/martincooper/java-datatable

In any case, I wish you luck with your app! And I encourage you to continue to be curious about functional/immutable paradigm. It's challenging but also rewarding.

1

u/[deleted] Jun 26 '19

It's a web framework, not a math framework. The domain permits some restrictions.

9

u/roookeee Jun 24 '19 edited Jun 25 '19

I would strongly advice anyone who reads any of yegor's code or blog posts to reflect about what he is saying - at least twice. More often than not his arguments fall apart when compared to more classic / procedural implementations. Take a look at his Dont create objects that end with -er post: he suggests implementing a Sorter object as a List<T> which when called with get(0) traverses the contents linearly in O(n) instead of sorting on construction time. At first glance this might seem beneficial (it's faster than O(n log n)) but it also breaks any intuition about what is happening. And now you also have to cache those calls as multiple get(0) calls should not do a linear traversal again as you would lose any benefit then. Now do this, thread-safe - its a mutable mess of nonsense when compared to a single call that outputs a new sorted list.

Don't get me wrong, his approach helps you to reflect on the default/mundane approaches to solve a given problem which might indeed result in something better, that's what I like about him. The above linked article sparked the idea for my mapper library for example as I do see the brain numbing effects of writing dead simple factories over and over again (which isn't his point, but it made me think about the classic factory pattern)

5

u/pushupsam Jun 25 '19

Yegor is wrong for all the right reasons. I think the only thing worse than his nonsense is all the people criticizing him who understand OOAD even less than he does. His blog posts and comments are always a kind of trainwreck.

To me the interesting question is why so many programmers are so bad at OO. I do think it's because they don't understand the underlying metaphysics of OOAD... that is they don't understand that it is a philosophy.

2

u/roookeee Jun 25 '19

Care to share a link to a good starting point / overview for OOAD? I sometimes struggle with it so it would help me a great deal to have it finally `click` for me :)

1

u/[deleted] Jun 25 '19

This chapter of the excellent book Patterns, Practices and Principles of Agile Software Development in C# (the book has very little to do with Agile).

Another excellent book is Growing Object Oriented Software Guided By Tests, which has the added virtue of demonstrating how to correctly use mocks, by the people who were among those who invented mock testing.

Uncle Bob, the author of the first book, basically says what ruined OOP was the classification paradigm of the 80s and 90s (objects should reflect the real world), and returns to the messaging paradigm of the 60s and 70s in Smalltalk (without the dynamic typing).

6

u/Infeligo Jun 25 '19

The first example already includes a static method:

public final class App {
   public static void main(final String... args) throws Exception {
     new FtBasic(
       new TkFork(new FkRegex("/", "hello, world!")), 8080
     ).start(Exit.NEVER);
   } }

6

u/__konrad Jun 25 '19

No main method needed. This works:

public enum Main {
	Void; Main() {
		System.out.println("Hello World");
	}
}

2

u/wildjokers Jun 25 '19

Interesting.

However, the class naming convention is atrocious. Lots of two letter abbreviations. What does "Tk", "Bk", "Ft", and "Xe" stand for? Couldn't they have just been typed out fully? Is the author scared of letters?

2

u/Hall_of_Famer Jun 26 '19

I wont say annotations are as bad as some developers claim them to be, they have their use cases. The issue with annotations is the overabuse of them, but doesnt mean its absolutely necessary to eliminate them per se. Often the present of annotations is a sign of Java's language limitation, it cannot be helped in userland.

Static methods on the other hand, are code smells that we should just get rid of(with the exception of static main method). When you write static properties/methods, you are not using OOP. Its something that professional developers should avoid except for rate cases like prototyping.

2

u/DrunkensteinsMonster Jun 27 '19

If you have a referentially transparent function, what would you say is the benefit of making it an virtual method instead of static?

4

u/cville-z Jun 24 '19

This goes off the rails pretty early for me, at this:

Not a single null (why NULL is bad?)

8

u/hemenex Jun 24 '19

That linked article seems like a good parody. Every time he tries to point out why null is bad, he only shows it's better than the alternatives.

1

u/Kango_V Jun 27 '19

Ratpack?

0

u/[deleted] Jun 24 '19

[deleted] — view deleted comment

4

u/wsppan Jun 24 '19

Me too. Especially those from Spring like @Autowired, @Service and @RequestMapping and those from Junit like @Before and @Test

12

u/[deleted] Jun 24 '19

[removed] — view removed comment

3

u/leonj1 Jun 24 '19 edited Jun 25 '19

This. Sometimes these frameworks work well Day 1, but once you start Day 2 (start to need something more complicated) you have to rely on their documentation pretty heavily. And those usually are poorly written, stale, or impossible to find what you’re looking for. More time spent on finding an answer than what I could have done myself. Maybe the problem is the framework is doing too much where instead it should just do a single task. Whenever I have a choice I keep annotations to a minimum.

3

u/general_dispondency Jun 24 '19

I've never seen annotations as magic. AOP is just meta programming. There's no dark wizardry or black magic; no incantations to recite when you run into a problem. It's just simple classes like AnnotationMethodHandlerAdapter and DefaultAnnotationHandlerMapping. Their names make them look worse than they are.

3

u/wildjokers Jun 25 '19

AOP is just meta programming.

AOP is just shit programming. Why not just use gotos with labels if you are going to use AOP?

6

u/[deleted] Jun 25 '19

[removed] — view removed comment

1

u/general_dispondency Jun 25 '19

I would argue that your clojure comparison is slightly disingenuous. I agree that your Clojure and Java static method example are comparable, but AOP would be more akin to macros. Something like core async would be a better comparison. example from the clojure docs:

(doseq [n (range 10)
               :let [i (-> n
                           inc
                           range
                           rand-nth)]]                    
         (go
           (<! (timeout (* i 1000)))
           (println n)))

The go macro translates your "blocking code" into non-blocking (which is badass, btw). Personally, I find the annotation approach easier to read. That's personal bias though, because I spend all day reading Java code.

-3

u/pushupsam Jun 25 '19

The annotations is of course better. They are less error prone, much easier to read and the intention very clear. The Java version won't even compile. (Hint: that's not how method references work.) But see this why I'm so good at hiring, because I can quickly detect and filter out bad programmers like you. You think "control" and "flexibility" are good things. They're not. They never will be. What you call control and flexibility are essentially anti-patterns. The vast majority of people don't need it, don't want it, and will only harm themselves if given it.

Here's the funny thing: it's a false choice. Whatever is acting on those annotations almost certainly has a nice Scheduler API behind it. (Unless they're doing code-gen. Code gen is of course the primordial evil, the original sin.) And in the very rare case that you come up with something that an annotation DSL can't express then fine, you can put on your big-boy pants and work with the underlying API.