Most Android apps follow the same pattern: a RESTful third-party service provides information represented as JSON and XML, and your task is to present that information and to communicate user actions to the service. Designing and understanding the way data flows into your architecture usually result in a much cleaner codebase. Here you'll be presented 5 useful insights that will help you write code that is a pleasure to read, easy to test and fun to change.
8. Keep you objects in shape
class Event {
private final String name;
private final Location where;
private final DateRange duration;
private final Type type;
public Event(String name, Location where, DateRange when, Type type) {
this.name = name;
this.where = where;
this.duration = when;
this.type = type;
if (name.isEmpty()) {
throw new IllegalArgumentException("Event name is empty");
}
if (type == WORKSHOP && when.includes(27TH_MARCH )) {
throw new IllegalArgumentException("Workshops are scheduled after 27th of March");
}
}
public boolean clashesWith(Event otherEvent) {
return duration.overlaps(otherEvent.duration) &&
where.equals(otherEvent.where);
}
}