Elegant Object 

Book by Yegor Bugayenko 

What is Object Oriented Programming? 

  • Opposition with procedural proramming
  • Use objects :

 

 

 

Data : attributes or properties

Code : procedures called methods

Birth of the objects

The Name

  • Name it for what it is and not for what it does

 

      Example : Cash  VS  CashConverter

The Constructor

  • One primary constructor and some secondary constructors
public class Cash{

	private int dollars;
    
    Cash(int dlr){
    	this.dollars=dlr;
    }    
    Cash(String dlr){
    	this(Cash.parse(dlr);
    }
    Cash(float dlr){
    	this((int)dlr);
    }
}

Education

The Encapsulation

  • Encapsulate a maximum of 4 objects --> stay clear

     

The Methods

  • Do not use static methods
  • Use interfaces for public methods --> decouple the objects (modify the object without changing the objects it interact with)
public class Client{

	private String name;
    private String address;
    private String birthdate;
}
public class Address{

	private int number;
    private String Street;
    private String City
    private int PostalCode;
}

Education

The Methods

  • Builders names are Nouns --> always return someting, but never void
  • Manipulator's names are Verbs --> always return void
class Pixel{	// Manipulator
    void Paint(Color color);        
}

Pixel center = new Pixel(50,50);
center.paint(new Color("red"));
  • The boolean should have adjectives for names.
class Human{

	private String Name;
	private int Age;
	
    public Human(String Name, int Age){
    	this.Name=Name;
        this.Age=Age;
    }
    
    public String getName(){
    	return Name;
    }
}

Constant

  • You should not use global constant (public static final), this is against the definition of Oriented Object Programming 

Immuability

  • The objects must be immutable in order to avoid bugs and thread safe
  • This is better to use small classes

ElegantObject

By Quentin Vaunac

ElegantObject

  • 65