Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Joshua Bloch in Effective Java argues that the java bean is an anti-pattern and should be avoided.


Oh thank God somebody in authority is calling this bean bull_hit what it is.

After reading the Eiffel book and thinking about things like programming by contract and class invariants, the bean pattern which constructs empty, initially useless (or at least unreliable) objects seemed like a huge step back.


Cool story, bro.

(Mime aside, do we get to hear what the argument was?)


The crux of the argument (and most of the book for that matter) is that you want to avoid mutability whenever possible. Adding setters for every field by default means mutability is the default mode for your application. Adding getters for every field by default means you lose any advantages of encapsulation. At one time, Java Beans were a heavily marketed pattern by Sun. Joshua Bloch came along and said "hey, this is a bad pattern" (and maybe others, but he's the one I always think of).


Why does everything have a getter/setter by default? That is a pretty horrible anti-pattern, is it something to do with being able to serialise the state (including internal state) of the whole object?


It's about hiding implementation. If you access an object only through methods then the implementation can change without the client code having to know or care (aka be recompiled)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: