Do One Thing Well

A function should have one job to do, and it should do that job really well. Every task required to do that one job should be performed by the function, and any task that is not related to that job should not be included in the function. Unrelated tasks can create bizarre side effects. Listing 8.9 demonstrates the type of strange behavior that might happen when unrelated tasks are included in a function.

Listing 8.9 Make the Bed (and Some Other Stuff)

function makeTheBed(bed) {  // Put the pillows on the bed  bed.pillowsOnBed = true;  // Put the sheets on the bed  bed.sheetsOnBed = true;  // Straighten the sheets  bed.sheetsStraightened = true;  // Put the blankets on the bed  bed.blanketsOnBed ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.