How much code should one put in a constructor?

前端 未结 9 2182
一整个雨季
一整个雨季 2020-12-15 05:34

I was thinking how much code one should put in constructors in Java? I mean, very often you make helper methods, which you invoke in a constructor, but sometimes there are s

相关标签:
9条回答
  • 2020-12-15 06:02

    Constructors should create the most minimal, generic instance of your object. How generic? Choose the test cases that every instance or object that inherits from the class must pass to be valid - even if "valid" only means fails gracefully (programatically generated exception).

    Wikipedia has a good description :

    http://en.wikipedia.org/wiki/Constructor_(computer_science)

    A Valid object is the goal of the constructor, valid not necessarily useful - that can be done in an initialization method.

    0 讨论(0)
  • 2020-12-15 06:03

    Your class may need to be initialized to a certain state, before any useful work can be done with it.

    Consider this.

    public class CustomerRecord
    {
        private Date dateOfBirth;
    
        public CustomerRecord()
        {
            dateOfBirth = new Date();
        }
    
        public int getYearOfBirth()
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOfBirth);
            return calendar.get(Calendar.YEAR);
        }   
    }
    

    Now if you don't initialize the dateOfBirth member varialble, any subsequent invocation of getYearOfBirth(), will result in a NullPointerException.

    So the bare minimum initialization which may involve

    1. Assigning of values.
    2. Invoking helper functions.

    to ensure that the class behaves correctly when it's members are invoked later on, is all that needs to be done.

    0 讨论(0)
  • 2020-12-15 06:05

    As little as is needed to complete the initialization of the object.

    If you can talk about a portion (5 or so lines is my guideline) of your constructor as a chunk of logic or a specific process, it's probably best to split it into a separate method for clarity and organizational purposes.

    But to each his own.

    0 讨论(0)
  • 2020-12-15 06:08

    If you go by the SOLID principles, each class should have one reason to change (i.e. do one thing). Therefore a constructor would normally not be reading a file, but you would have a separate class that builds the objects from the file.

    0 讨论(0)
  • 2020-12-15 06:09

    Constructors should be just long enough, but no longer =)

    If you are defining multiple overloaded constructors, don't duplicate code; instead, consolidate functionality into one of them for improved clarity and ease of maintenance.

    0 讨论(0)
  • 2020-12-15 06:14

    My customary practice is that if all the constructor has to do is set some fields on an object, it can be arbitrarily long. If it gets too long, it means that the class design is broken anyway, or data need to be packaged in some more complex structures.

    If, on the other hand, the input data need some more complex processing before initializing the class fields, I tend to give the constructor the processed data and move the processing to a static factory method.

    0 讨论(0)
提交回复
热议问题