词条 | Cohesion (computer science) |
释义 |
In computer programming, cohesion refers to the degree to which the elements inside a module belong together.{{sfn|Yourdon|Constantine|1979}} In one sense, it is a measure of the strength of relationship between the methods and data of a class and some unifying purpose or concept served by that class. In another sense, it is a measure of the strength of relationship between the class’s methods and data themselves. Cohesion is an ordinal type of measurement and is usually described as “high cohesion” or “low cohesion”. Modules with high cohesion tend to be preferable, because high cohesion is associated with several desirable traits of software including robustness, reliability, reusability, and understandability. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or even understand. Cohesion is often contrasted with coupling, a different concept. High cohesion often correlates with loose coupling, and vice versa.[1] The software metrics of coupling and cohesion were invented by Larry Constantine in the late 1960s as part of Structured Design, based on characteristics of “good” programming practices that reduced maintenance and modification costs. Structured Design, cohesion and coupling were published in the article {{harvtxt|Stevens|Myers|Constantine|1974}} and the book {{harvtxt|Yourdon|Constantine|1979}}; the latter two subsequently became standard terms in software engineering. High cohesion{{unreferenced section|date=October 2014}}In object-oriented programming, if the methods that serve a class tend to be similar in many aspects, then the class is said to have high cohesion.[2] In a highly cohesive system, code readability and reusability is increased, while complexity is kept manageable. Cohesion is increased if:
Advantages of high cohesion (or "strong cohesion") are:
While in principle a module can have perfect cohesion by only consisting of a single, atomic element – having a single function, for example – in practice complex tasks are not expressible by a single, simple element. Thus a single-element module has an element that either is too complicated, in order to accomplish a task, or is too narrow, and thus tightly coupled to other modules. Thus cohesion is balanced with both unit complexity and coupling. Types of cohesionCohesion is a qualitative measure, meaning that the source code to be measured is examined using a rubric to determine a classification. Cohesion types, from the worst to the best, are as follows:
/* Groups: The function definitions Parts: The terms on each function
Module A{ /* Implementation of r(x) = 5x + 3 There is no particular reason to group functions in this way, so the module is said to have Coincidental Cohesion. */ r(x) = a(x) + b(x) a(x) = 2x + 1 b(x) = 3x + 2 }
/* Groups: The function definitions Parts: The terms on each function
Module A { /* Implementation of arithmetic operations This module is said to have functional cohesion because there is an intention to group simple arithmetic operations on it. */ a(x, y) = x + y b(x, y) = x * y } Module B { /* Module B: Implements r(x) = 5x + 3 This module can be said to have atomic cohesion. The whole system (with Modules A and B as parts) can also be said to have functional cohesion, because its parts both have specific separate purposes. */ r(x) = [Module A].a([Module A].b(5, x), 3) }
/* Groups: The function definitions Parts: The terms on each function
Module A { /* Implementation of r(x) = 2x + 1 + 3x + 2 It´s said to have perfect cohesion because it cannot be reduced any more than that. */ r(x) = 5x + 3 } Although cohesion is a ranking type of scale, the ranks do not indicate a steady progression of improved cohesion. Studies by various people including Larry Constantine, Edward Yourdon, and Steve McConnell [3] indicate that the first two types of cohesion are inferior; communicational and sequential cohesion are very good; and functional cohesion is superior. While functional cohesion is considered the most desirable type of cohesion for a software module, it may not be achievable. There are cases where communicational cohesion is the highest level of cohesion that can be attained under the circumstances.{{Citation needed|date=March 2012}} See also
Citations1. ^{{cite book | last = Ingeno | first = Joseph | author-link = | title = Software Architect’s Handbook | publisher = Packt Publishing | series = | volume = | edition = | date = 2018 | location = | pages = 175 | language = | url = | doi = | id = | isbn = 1788624068 | mr = | zbl = | jfm = }} 2. ^Marsic (2012). Software Engineering. Rutgers University 3. ^Code Complete 2nd Ed., p168-171 References{{refbegin}}
External links
3 : Software architecture|Software metrics|Programming principles |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。