Improving Code Quality - Session 39: Passing Data Before the Task
Key point
It suggests explicitly defining task order by leveraging function arguments and return values, thereby increasing cohesion and reducing coupling.
Details
When there is a constraint that a particular function must always be called after another function has executed, failing to make this explicit risks runtime errors. For example, in a media playback class, this happens when the playback function (playMedia) is called without first calling the initialization function (initMedia).
The best way to resolve this kind of implicit dependency is to make the constraint explicit using arguments and return values. By designing the code so that the result of the initialization function is passed as an argument to the next function, developers are prevented from accidentally breaking the order and are instead forced to call things in the correct sequence.
This kind of design improves both Cohesion and Coupling in the code at the same time.
- Cohesion: It evolves from 'procedural strength,' which simply operates in a fixed order, to 'communicational strength,' where data flows through, thereby increasing cohesion.
- Coupling: It weakens from 'Common Coupling,' where instance properties are shared, to 'Stamp Coupling,' where structured values are passed, thereby reducing coupling.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.