7. Effective Java #2
Consider a builder when faced with
many constructor parameters
www.canoo.com
8. Person p = new Person(1, "David", "Villa");
www.canoo.com
9. Person p = new Person(1, "David", "Villa");
Person p = new PersonBuilder().
withID(1).
withFirstName("David").
withLastName("Villa").
build();
www.canoo.com
10. public class PersonBuilder {
private String firstName
private String lastName
private Integer id
public PersonBuilder withID(int id) {
this.id = id; return this;
}
public PersonBuilder withFirstName(String firstName) {
this.firstName = firstName; return this;
}
public PersonBuilder withLastName(String lastName) {
this.lastName = lastName; return this;
}
public Person build() {
return new Person(id, firstName, lastName);
}
}
www.canoo.com
11. def p = new Person(
id: 1,
firstName: 'David',
lastName: 'Villa'
)
www.canoo.com
12. def p = new Person().with {
id = 1
firstName = 'David'
lastName = 'Villa'
delegate
}
www.canoo.com
13. Effective Java #3
Enforce the singleton property
with a private constructor
www.canoo.com
45. Effective Java #56
Adhere to generally accepted
naming conventions
www.canoo.com
46. Effective Java #56
Adhere to generally accepted
naming conventions
www.canoo.com
47. Effective Java #57 – #61
Use exceptions only for exceptional conditions
Avoid unnecessary use of checked exceptions
Use checked exceptions for recoverable conditions
...
www.canoo.com
48. Effective Java #57 – #61
Use exceptions only for exceptional conditions
Avoid unnecessary use of checked exceptions
Use checked exceptions for recoverable conditions
… and more
www.canoo.com
55. List<Person> people = Person.findAll();
List<Integer> ids = new ArrayList<Integer>();
for (Person p : people) {
ids.add(p.getId());
}
www.canoo.com
59. List<Person> people = Person.findAll();
Person joe = null;
for (Person p : people) {
if ("Joe".equals(p.getFirstName())) {
joe = p;
break;
}
}
www.canoo.com
60. def people = Person.findAll()
def joe = people.find { it.firstName == 'Joe' }
www.canoo.com
61. List<Person> people = Person.findAll();
List<Person> bucks = new ArrayList<Person>();
for (Person p : people) {
if ("Buck".equals(p.getLastName())) {
bucks.add(p);
}
}
www.canoo.com
105. More Effective Java
#21: Use function objects to represent strategies
#36: Consistently use @Override
#71: Use Lazy Initialization judiciously – See Groovy's @Lazy
#47: Know & use the libraries – Read the GDK Docs and Release Notes
#63: Include Failure-capture information in detailed messages
#11: Override Clone Judiciously – See @AutoClone, @Canonical
www.canoo.com
106. More Effective Groovy
#9: Learn to Write a Builder
#10: XMLSlurper/Parser - Do not mix with business logic/layers/etc
#11: Effective Java #21: Use Function Objects to represent strategies
#12: Threading: Avoid Busy Wait
#13: Threading: Avoid Double Checked Locking
#14: Threading: Avoid Inconsistent Property Locking
#15: Threading: Avoid Inconsistent Property Synchronization
#16: Threading: Avoid Synchronizing On Boxed Primitive
#17: Know and use Elvis operator ?:
#18: Excessively use the null-safe dereference operator
#19: Understand Operator Overloading
www.canoo.com