views:

433

answers:

5

I'm trying to tell someone his code is not "coherent" in the sense that it serves multiple purposes. I don't think I can explain it very well, so I'm looking for a good reference and/or definition.

+6  A: 

I think the correct term is cohesion.

In computer programming, cohesion is a measure of how strongly-related and focused the various responsibilities of a software module are. Cohesion is an ordinal type of measurement and is usually expressed as "high cohesion" or "low cohesion" when being discussed.

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 whereas low cohesion is associated with undesirable traits such as being difficult to maintain, difficult to test, difficult to reuse, and even difficult to understand.

starblue
Ah. It seems Google can't pass the Turing Test... yet. No wonder I couldn't find the answer there! Accepted for the link.
allyourcode
A: 

Marked by an orderly, logical, and aesthetically consistent relation of parts; "a coherent argument" - from http://www.websters-online-dictionary.org/definition/coherent

tehvan
Not what I was looking for.
allyourcode
+3  A: 

I had Code Complete by Steve McConnell next to my computer (ie the programmers bible) with the page open explaining cohesion so I thought I'd share,

Cohesion arose from structured design and is usually discussed in the same context as coupling. Cohesion refers to how closely all the routines in a class or all the code in a routine support a central purpose-how focused the class is. Classes that contain strongly related functionality are described as having strong cohesion, and the heuristic goal is to make cohesion as strong as possible.

CodeKiwi
+1. Add some links though. Code Complete website is here http://www.cc2e.com/, and why not link "programmers bible" to this SO question, where it wins by miles http://stackoverflow.com/questions/1711/what-is-the-single-most-influential-book-every-programmer-should-read
MarkJ
@CodeKiwi You have _Code Complete_ open next to you at all times? That's a little creepy. Thanks for the reference though!
allyourcode
haha, No I am currently reading through it, it probablly wouldnt hurt to do so though, its a great book.
CodeKiwi
+1  A: 

I use the term “separation of concerns” to explain this while refactoring. Often when code is fairly new, things will get lumped in together as the separate concerns are not clear at first.

One easy way to illustrate this to your co worker would be to ask them to write test cases for the code. This should illustrate that the code is not clear or coherent.

Another good phrase to use is that functions/objects “should do one thing, and do it well”, this has implications in everything from the object/method names to the overall architecture of the system.

Jeremy French
A: 

In addition to the answers given so far, a simple way to think of high cohesion is lack of duplication of functionality, and clear seperation of related functionality into distinct modules, components or classes. Thus if you want a function similar to another function, and you cut and paste and subsequently modify a copy of the code, you are reducing cohesion. If you modify the the original to handle the new case, where the new case is clearly related to the existing functionality, you are increasing cohesion. Put another way, if your program has to do a given thing, no matter how times or in how many places, for maximum cohesion there should only be once piece of code that does that thing. At the same, a given class, module or component should have a single area of responsibility. Lumping unrelated functionality into a single class or component also reduces cohesion.

As CodeWiki says, cohesion is typically discussed with coupling, where the two can act in opposition to one another, particularly where strict interfaces aren't carefully planned. Many of the googled articles on cohesion relate to OO design, but cohesion and coupling are not restricted to OO.

Shane MacLaughlin
Wouldn't generalizing a function make it _less_ cohesive in some cases? I think that's the problem with my colleague's code: it tries to accomplish to much instead of "doing one thing well", as suggested by another answer.
allyourcode
Sure, there is a fine line between unncessary replication and over generalisation. The former is regularly the problem as it is so easy to achieve. Your point is valid though, and I've edited my post slightly to cover your concerns.
Shane MacLaughlin

related questions