Skip to main content
added 563 characters in body
Source Link
Danny Varod
  • 1.2k
  • 6
  • 15

The answer depends on how you use the class...

  • If you want your class to be immutable, you'll have to use constructor parameters.

  • If you want your class to be deserializable by relection based (de)serializers, you will need a default constructor and setters.

  • If your class isn't supposed to be immutable and you want consumers to be able to change all the properties during the objects' lifetime, then you will need to add setters anyway (or add modification methods, which may seem less straightforward to consumers).

  • If you do use the default constructor and properties approach, then you must make sure that the consumers can not invalidate the object's state by setting in an unexpected order, that default values are valid and that all the setters check things like ranges before applying the changes.
    If you have two (or more) dependant properties, either encapsulate them in a class, or make sure that if either of them is set (e.g. X is set), it changes the other's value (e.g. Y's value) if (Y's) current value doesn't not go together with the new value (of X).


Also, consider if your class really should be one class:

If it has 30 parameters, you may be breaking the OO principle of encapsulation - where each class does one thing and does everything for that one thing.

Perhaps you should be using composition of a few classes or, if you have a lot of common parameters between classes, perhaps you need one or more vertical classes that handles a specific aspect and can act on multiple classes (either using reflection or using additional classes in a visitor pattern).

The answer depends on how you use the class...

  • If you want your class to be immutable, you'll have to use constructor parameters.

  • If you want your class to be deserializable by relection based (de)serializers, you will need a default constructor and setters.

  • If your class isn't supposed to be immutable and you want consumers to be able to change all the properties during the objects' lifetime, then you will need to add setters anyway (or add modification methods, which may seem less straightforward to consumers).


Also, consider if your class really should be one class:

If it has 30 parameters, you may be breaking the OO principle of encapsulation - where each class does one thing and does everything for that one thing.

Perhaps you should be using composition of a few classes or, if you have a lot of common parameters between classes, perhaps you need one or more vertical classes that handles a specific aspect and can act on multiple classes (either using reflection or using additional classes in a visitor pattern).

The answer depends on how you use the class...

  • If you want your class to be immutable, you'll have to use constructor parameters.

  • If you want your class to be deserializable by relection based (de)serializers, you will need a default constructor and setters.

  • If your class isn't supposed to be immutable and you want consumers to be able to change all the properties during the objects' lifetime, then you will need to add setters anyway (or add modification methods, which may seem less straightforward to consumers).

  • If you do use the default constructor and properties approach, then you must make sure that the consumers can not invalidate the object's state by setting in an unexpected order, that default values are valid and that all the setters check things like ranges before applying the changes.
    If you have two (or more) dependant properties, either encapsulate them in a class, or make sure that if either of them is set (e.g. X is set), it changes the other's value (e.g. Y's value) if (Y's) current value doesn't not go together with the new value (of X).


Also, consider if your class really should be one class:

If it has 30 parameters, you may be breaking the OO principle of encapsulation - where each class does one thing and does everything for that one thing.

Perhaps you should be using composition of a few classes or, if you have a lot of common parameters between classes, perhaps you need one or more vertical classes that handles a specific aspect and can act on multiple classes (either using reflection or using additional classes in a visitor pattern).

added 560 characters in body
Source Link
Danny Varod
  • 1.2k
  • 6
  • 15

The answer depends on how you use the class...

  • If you want your class to be immutable, you'll have to use constructor parameters.

  • If you want your class to be deserializable by relection based (de)serializers, you will need a default constructor and setters.

  • If your class isn't supposed to be immutable and you want consumers to be able to change all the properties during the objects' lifetime, then you will need to add setters anyway (or add modification methods, which may seem less straightforward to consumers).


If you wantAlso, consider if your class toreally should be immutable, you'll have to use constructor parameters.one class:

If it has 30 parameters, you want your class tomay be deserializable by relection basedbreaking the OO principle of encapsulation (de)serializers, you will need a default constructor- where each class does one thing and settersdoes everything for that one thing.

If your class isn't supposed to be immutable andPerhaps you want consumers toshould be able to change all the properties during the objects' lifetimeusing composition of a few classes or, thenif you willhave a lot of common parameters between classes, perhaps you need to add setters anywayone or more vertical classes that handles a specific aspect and can act on multiple classes (or add modification methods, which may seem less straightforward to consumerseither using reflection or using additional classes in a visitor pattern).

The answer depends on how you use the class...

If you want your class to be immutable, you'll have to use constructor parameters.

If you want your class to be deserializable by relection based (de)serializers, you will need a default constructor and setters.

If your class isn't supposed to be immutable and you want consumers to be able to change all the properties during the objects' lifetime, then you will need to add setters anyway (or add modification methods, which may seem less straightforward to consumers).

The answer depends on how you use the class...

  • If you want your class to be immutable, you'll have to use constructor parameters.

  • If you want your class to be deserializable by relection based (de)serializers, you will need a default constructor and setters.

  • If your class isn't supposed to be immutable and you want consumers to be able to change all the properties during the objects' lifetime, then you will need to add setters anyway (or add modification methods, which may seem less straightforward to consumers).


Also, consider if your class really should be one class:

If it has 30 parameters, you may be breaking the OO principle of encapsulation - where each class does one thing and does everything for that one thing.

Perhaps you should be using composition of a few classes or, if you have a lot of common parameters between classes, perhaps you need one or more vertical classes that handles a specific aspect and can act on multiple classes (either using reflection or using additional classes in a visitor pattern).

Source Link
Danny Varod
  • 1.2k
  • 6
  • 15

The answer depends on how you use the class...

If you want your class to be immutable, you'll have to use constructor parameters.

If you want your class to be deserializable by relection based (de)serializers, you will need a default constructor and setters.

If your class isn't supposed to be immutable and you want consumers to be able to change all the properties during the objects' lifetime, then you will need to add setters anyway (or add modification methods, which may seem less straightforward to consumers).