Explore key principles of cohesion and coupling with scenario-based questions designed to reinforce essential software engineering concepts. Enhance your understanding of how module design decisions impact maintainability, flexibility, and reliability.
In the context of software design, which type of cohesion is considered the most desirable when designing a module that processes a single well-defined task?
Explanation: Functional cohesion is the most desirable because it means all parts of the module work together to perform a single, specific function, which maximizes clarity and maintainability. Procedural cohesion is less preferable since it only groups functions by the order of execution, not by shared purpose. Coincidental cohesion, where unrelated operations coexist, is unfavorable and can confuse maintenance. Temporal cohesion groups elements by timing only, which does not ensure logical relatedness.
Which scenario best illustrates tight coupling between two components in software architecture?
Explanation: Tight coupling occurs when one component relies on the internal details of another, such as directly accessing and changing its variables, making maintenance and testing harder. Merely calling public methods through an interface allows for better abstraction and looser coupling. Sharing naming conventions does not establish operational dependency. Using a message queue facilitates loose coupling by separating components through asynchronous communication.
What is a likely consequence when a module in a program exhibits low cohesion, such as handling database access, UI display, and logging in one place?
Explanation: Low cohesion results in modules combining unrelated functionality, which complicates understanding, maintenance, and testing efforts. Combining logic rarely improves performance and is likely to introduce inefficiencies instead. Such a module violates the single responsibility principle. Additionally, grouping unrelated tasks does not reduce dependencies but may inadvertently increase them.
Which type of coupling is considered the weakest and thus most desirable when designing modules that exchange data?
Explanation: Data coupling is the weakest form, involving only the exchange of simple data through parameters, promoting modular and maintainable design. Content coupling is the strongest and least desirable, as it exposes module internals. Control coupling occurs when one module controls another’s logic, which should be avoided for flexibility. Common coupling, using shared global data, increases inter-module dependencies and risks side effects.
During a code review, you notice a class that validates user input, handles network requests, and writes to a log file. What should you do to improve its cohesion?
Explanation: Separating each responsibility into its own class increases functional cohesion, making code more modular and easier to maintain. Renaming the class only improves clarity but does not address the core problem. Adding comments may help understanding, but does not solve the issue of multiple responsibilities. Combining more tasks further reduces cohesion and makes the codebase more complex and fragile.