Ho letto dal seminale codice Completato il libro che le istruzioni del metodo che richiedono di essere eseguite nell'ordine che passa parametro da uno a uno è un odore di codice ed è un esempio di coesione sequenziale . Perché non è una buona idea?
Un esempio forzato di coesione sequenziale:
public Part createPart(input) {
PartOne partOne = computePartOne(input);
PartTwo partTwo = computePartTwo(partOne);
PartThree partThree = computePartThree(partTwo);
PartsBuilder partsBuilder = new PartsBuilder();
return partsBuilder.add(partOne).add(partTwo).add(partThree).build();
}
Ecco l'azione:
Several other kinds of cohesion are normally considered to be less than ideal:
Sequential cohesion exists when a routine contains operations that must be performed in a specific order, that share data from step to step, and that don't make up a complete function when done together.
An example of sequential cohesion is a routine that, given a birth date, calculates an employee's age and time to retirement. If the routine calculates the age and then uses that result to calculate the employee's time to retirement, it has sequential cohesion. If the routine calculates the age and then calculates the time to retirement in a completely separate computation that happens to use the same birth-date data, it has only communicational cohesion.
How would you make the routine functionally cohesive? You'd create separate routines to compute an employee's age given a birth date and compute time to retirement given a birth date. The time-to-retirement routine could call the age routine. They'd both have functional cohesion. Other routines could call either routine or both routines.