There's no point in creating objects that don't effect program outcome. The creation of such objects is likely to indicate an error. Perhaps an assignment was forgotten or a method left uncalled?


This rule raises an issue when an object is created and its state is updated, but it neither leaves the method where it was created nor affects the enclosing object.


== Noncompliant Code Example

----
public void addPerson(String name){
  Person p = new Person();  // Noncompliant
  p.gender = Person.Gender.MALE;
  p.setName(name);
}
----


== Compliant Solution

----
public void addPerson(){
  Person p = new Person();
  p.gender = Person.Gender.MALE;
  p.setName("Sam");

  people.add(p);
}
----


