Teaser Image

qnoid

Markos Charatzas - Edinburgh, UK




public class Plane
    {
        private boolean engineStarted;

        /**
         * Don't do that
         */
        public void init()
        {
            this.startEngine();
        }
        
        /**
         * 
         */
        public void startEngine(){
            this.engineStarted = true;
        }
    }

    public class PlaneFactory
    {
        /**
         * @return a new {@link Plane} ready to take off
         */
        public Plane newPlane()
        {
            Plane plane = new Plane();
            plane.startEngine();
            
        return plane;
        }
    }

Don't write #init methods. They only add an extra place for the client code to look for and lead to frustration when trying to figure out what went wrong.

Instead use a factory method to return an object to the required state and document that.