Jan 26, 2012

Importance of Cohesion & Coupling in Software Design

,

Cohesion and coupling both measure the relationship between elements of computer software. Cohesion measures how the components of a software module or a class of software objects relate to each other. Coupling measures the relationship between separate classes and modules. Good programming increases cohesion and decreases the amount of coupling.

  1. Cohesion

    • A class or module has high cohesion if all the responsibilities, data and methods it includes are closely related. If a project-scheduling module includes only methods or functions relating to scheduling, it's highly cohesive; if it also includes org charts and a customer database, it's less cohesive. To measure cohesion, calculate the number of shared fields that methods -- subroutines -- in a class have to access and compare it to the number of unrelated fields they use. The greater the number of common fields, the greater the cohesion.

    Significance

    • If a module has to handle multiple operations -- security, infrastructure, network rules -- it becomes harder to work with than if everything in the module focuses on a single purpose. With a low-cohesion model, you may have to make multiple unrelated changes because each part needs a different fix or patch. Conversely, if related functions are divided into several files rather than a single cohesive files, you may have to make repeated identical changes to each file instead of a single fix to a single file.

    Coupling

    • When your software design uses tight coupling between classes or subsystems, that means they're interconnected: System A can't carry out its job without knowing details about what System B is doing, and a change to B means A has to change as well. With loose coupling, you can change System A more easily. It's also harder to study the code for A if you can't understand it without analyzing the code for B and their relationship.

    Efficiency

    • Low coupling makes coding more efficient because you can reuse more code: If System A and System B aren't interdependent, you can copy the code for one of them into another program without difficulty. Software that includes duplicate coding for duplicate functions is another sign of inefficiency. You're often better off if you rewrite the program to eliminate duplicate functions, then assign the function to its own cohesive module. The lower the level of coupling, the easier such sweeping changes become.

0 commentaires to “Importance of Cohesion & Coupling in Software Design”

Post a Comment

 

HowToYo Copyright © 2011 | Template design by O Pregador | Powered by Blogger Templates