30

Preserve Whole Object


Goal

Instead of pulling several values out of an object to pass them in, pass the object itself.

Before the refactoring
if (room.lowTemp < range.low || room.highTemp > range.high) { /* ... */ }
After the refactoring
if (range.includes(room)) { /* ... */ }
Savings

Signatures shrink; adding a needed field is internal; consumers don't have to plumb new arguments through.

Note

Passing the whole object adds coupling to its full surface — only do this when the called function might reasonably need other parts of the object.