When I have an Object that has lots of fields is it better to have them all as fields or try to find logical groupings as their own Objects and make those the fields?
I guess it comes down to which is the greater evil, too many Objects or too many fields in an Object.
Less Objects, more fields:
public class LotsOfFields Object {
private Foo foo;
private Bar bar;
private Baz baz;
private Dog dog;
private Cat cat;
private Goat goat;
// Getters and setters...
}
More Objects, less fields:
public class LessFields Object {
private ClicheMetaSyntacticVariables vars;
private Animals animals;
// Getters and setters...
}
public class ClicheMetaSyntacticVariables {
private Foo foo;
private Bar bar;
private Baz baz;
// Getters and setters...
}
public class Animals {
private Dog dog;
private Cat cat;
private Goat goat;
// Getters and setters...
}
As an aside, if the "more Objects" approach is better, is it better to have the classes as members of the original class (assuming they'd never be used elsewhere) or does that defeat the purpose of not cluttering an Object?
Edit to defend my question not being a duplicate: This is about a more specific issue than "what is the purpose of a class" and to me that alone is enough. To go on, I am really talking more about what makes a more readable/usable class, I'll take the blame for not specifying that though.