12 October 2006

An unsung benefit of Tell, Don't Ask

The Tell Don't Ask programming style forces you to represent and name interactions between objects. This is a great help during maintenance. The code better expresses what it is doing, not just how it is doing it. The following code promotes an employee to be manager of their department:
employee.getJobDescription().getResponsibilities().add(new ManagementResponsibility(employee.getDepartment()));
But a Tell Don't Ask style forces us to state that explicitly:
employee.promoteToManager();

No comments: