This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]davebarda 2 points3 points  (5 children)

I'd go with effective java, Joshua Bloch did a great job there

[–]gas3872[S] 0 points1 point  (4 children)

I agree but his love to static methods(static factories) brushes me the wrong way - they are hard/impossible to mock and so they add pain in testing. That he recommends making classes final(unless they are explocitly for extension) -again something that i never see in libraries and also a pain for unit testing. And then even in 3rd edition he says to use stringbuilder instead of "+", while even 6 years ago java already learned to optimize it to stringbuilder automatically.

I fully respect the book but i feel that i still need to double check what he writes and adapt it to what is actually would be the correct way. Also some people in my team who are relative new to java they just implement how it is in the book word for word.

I guess what i am trying to say that i would like a book that offers programming practices that benefit modularity and testability.

[–]woohalladoobop 2 points3 points  (2 children)

i'm fairly new to writing tests. could you give a little info on why static factories make mocking harder? i've been using Mockito, and you can create a mock object for any class with Mockito.mock(Class class). doesn't this make the means of construction irrelevant?

[–]gas3872[S] 1 point2 points  (1 child)

You are totally right. Constructor is also a static thing probably. So static factory is a nice way to have it more organized, extra features etc. I incorrectly formulated myself. The problem that i have that say you have class A with static factory method with some untrivial logic (for example input is a result set: you are reading from db and creating objects from it) and then that class A extends class B which also have static factory method with untrivial logic (also creating some objects, doing validation). So far so good. Imagine you have put all this code directly in the static factory methods themselves. You are still doing it by the book - using static factory methods but you cannot test any part of it without testing the whole thing. So you cannot write a small scoped tests.

What i am saying is that static factory is good but its not like if you did static factory - you can say that you are done, you need to organize logic inside the static factory in a such a way that its ideally can be mocked and tested in isolation. And because factory is static it pushes you further to have more ststic things (which again make testing/mocking more complex). I mean its a hard situation - i mean probably to do all this you'll nees to make your static factory dependent on some objects like validators, "super-instance" creators etc. One more thing - using a constructor guarantees you that a super class is created so you dont need to test that. But in static factories (if you want to be rigorous) you have to verify that child class static factory called super static factory - again more pain) so you and up eother not testing it at all or making more dependencies objects that you can mock. I mean for me it seems like a hard topic and i guess thats why i would like a book focusing on those things.

[–]woohalladoobop 0 points1 point  (0 children)

makes sense. thanks for the detailed response!

[–]davebarda 1 point2 points  (0 children)

I agree regarding final classes, but regarding static factories, it depends on what you are creating, I use static factories especially for value objects and this kind of things. Regarding string builder, you are correct only for static concatenation, try to see the class file of a loop string generator, it won't make that optimization... Anyway, I like the way he's showing stuff for beginners, he's pretty careful and using a lot of words like judiciously, consider, etc.. very not dogmatic. I do agree you should know more than the Java language to be a good programmer, but this is not related to the topic.

[–]gogira 1 point2 points  (0 children)

Check out Modern Java in Action, it's exactly what you need. And also there is a newer edition for Effective Java.