When abstractions attack
One of the most important programming skills is the ability to make abstractions, and there are some quite talented programmers out there. Unfortunately many use their powers of abstraction for evil instead of good. Consider the following Java:
public class ActionServlet extends HttpServlet
{
private ActionService actionService;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
Action action = actionService.createAction(request.getParameter("actionCode"));
action.setNote(request.getParameter("actionNote"));
action.setTargetId(request.getParameter("actionTargetId"));
action.setActionDateAndTime(toDateAndTime(request.getParameter("actionDate")));
actionService.handleAction(action);
....
}
.....
}
Nice! They’ve abstracted out the domain. I bet if you follow the code into handleAction() you’ll just find more framework. I come across too much code in which I can’t find the domain for the design.
This is the kind of thing I’d prefer to see:
public class ParcelArrivedServlet extends HttpServlet
{
private ParcelRepository parcelRepository;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
Parcel parcel = parcelRepository.findParcel(request.getParameter("parcelId"));
parcel.hasArrivedAt(toDateAndTime(request.getParameter("arrivalDateAndTime")))
.withDeliveryNote(request.getParameter("deliveryNote"));
.....
}
.....
}

0 Comments:
Post a Comment
<< Home