News for hackers. Stuff that matters.
Re:Write More Classes by dougk16 on Tuesday February 12, 2013 @01:46AM (#5205546)
I get what you're saying, but good OO for me usually suggests nullary constructors, letting a class know when something happens as opposed to setting internal state directly, and the class saying let me do something for you using my state instead of giving you my state directly.

Providing a getter at least is sometimes practically unavoidable, so there's 1 repetition, but with a good IDE that's just a quick key combination over the variable name.

Re:Write More Classes by epidemian on Tuesday February 12, 2013 @02:21AM (#5205639)
> good OO for me usually suggests nullary constructors

I'm curious, could you please elaborate on this point? If a class has any dependency, i usually find it better to require that dependency to be passed in the constructor, so the constructed instance can always be in a valid initialized state. Why do you find nullary constructors to be good OO?

Re:Write More Classes by dougk16 on Tuesday February 12, 2013 @10:13AM (#5207262)
I came off too heavy-handed there. My intention was more to just say that variables shouldn't be put in constructors if they don't have to be. So, just railing more against the pattern that Java seems to have popularized where every private variable automatically has a getter/setter and can be set through the constructor. Certainly if an object requires some initial state that can never change, passing the value in through the constructor is usually most appropriate. Even then, I sometimes like to have a nullary constructor with an init() method. Just makes certain design patterns utilizing e.g. reflection or pooling a bit easier to manage in some languages.
Re:Write More Classes by epidemian on Tuesday February 12, 2013 @06:05PM (#5210176)
Ok, i didn't get your point then. Well, i definitely agree on that the pattern/trend of auto-generating getters, setters and constructor parameters for every attribute in a class without considering if those attributes should be visible (or, worse, mutable) from the outside is pretty hideous.

About the nullary constructor + init() method for deserializing purposes, i don't have any strong opinion really, as long as consistency is kept throughout the code base/module that relies on that. Using Java's reflection API though, you can extract all necessary type information from non-nullary constructors in order to call them with the required dependencies, which is not essentially more complicated than instantiating the class and then setting its properties thought setter methods.

Re:Write More Classes by grn on Tuesday February 12, 2013 @02:23AM (#5205645)
I don't think that nullary constructors are so good. If you have dependencies then after constructing your object it'll be in an invalid state because the dependencies aren't set up correctly.
Re:Write More Classes by dougk16 on Tuesday February 12, 2013 @10:15AM (#5207270)
http://news.ycombinator.com/item?id=5207262

Is there a preferred way on HN to address a "duplicate" reply?