What is high cohesion and how to use it / make it?

后端 未结 10 2147
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 05:17

I\'m learning computer programming and at several places I\'ve stumbled upon the concept of cohesion and I understand that it is desirable for a software to have \"high cohe

相关标签:
10条回答
  • 2020-12-02 05:33

    MSDN's article on it is probably more informative than Wikipedia in this case.

    0 讨论(0)
  • 2020-12-02 05:34

    Cohesion is usually measured using one of the LCOM (Lack of cohesion) metrics, the original LCOM metric came from Chidamber and Kemerer. See for example: http://www.computing.dcu.ie/~renaat/ca421/LCOM.html

    A more concrete example: If a class has for example one private field and three methods; when all three methods use this field to perform an operation then the class is very cohesive.

    Pseudo code of a cohesive class:

    class FooBar {
      private SomeObject _bla = new SomeObject();
    
      public void FirstMethod() {
        _bla.FirstCall();
      }
    
      public void SecondMethod() {
        _bla.SecondCall();
      }
    
      public void ThirdMethod() {
        _bla.ThirdCall();
      }
    }
    

    If a class has for example three private fields and three methods; when all three methods use just one of the three fields then the class is poorly cohesive.

    Pseudo code of a poorly cohesive class:

    class FooBar {
      private SomeObject _bla = new SomeObject();
      private SomeObject _foo = new SomeObject();
      private SomeObject _bar = new SomeObject();
    
      public void FirstMethod() {
        _bla.Call();
      }
    
      public void SecondMethod() {
        _foo.Call();
      }
    
      public void ThirdMethod() {
        _bar.Call();
      }
    }
    

    The class doing one thing principle is the Single Responsibility Principle which comes from Robert C. Martin and is one of the SOLID principles. The principle prescribes that a class should have only one reason to change.

    Staying close to the Single Responsibility Principle could possibly result in more cohesive code, but in my opinion these are two different things.

    0 讨论(0)
  • 2020-12-02 05:34

    A general way to think of the principle of cohesion is that you should locate a code along with other code that either depend on it, or upon which it depends. Cohesion can and should be applied to levels of composition above the class level. For instance a package or namespace should ideally contain classes that relate to some common theme, and that are more heavily inter-dependent than dependent on other packages/namespaces. I.e. keep dependencies local.

    0 讨论(0)
  • 2020-12-02 05:34

    Most of the answers don't explain what is cohesion, It is well defined in uncle bobs book clean code.

    Classes should have a small number of instance variables. Each of the methods of a class should manipulate one or more of those variables. In general the more variables a method manipulates the more cohesive that method is to its class. A class in which each variable is used by each method is maximally cohesive. In general it is neither advisable nor possible to create such maximally cohesive classes; on the other hand, we would like cohesion to be high. When cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole.

    Let me explain it with a class definition

    class FooBar {
    private _bla;
    private _foo;
    private _bar;
    
    function doStuff()
    
       if(this._bla>10){
         this._foo = 10;
         this._bar = 20;
       }
    
    }
    function doOtherStuff(){
    
        if(this._foo==10){
           this._bar = 100;
           this._bla = 200;
        }
    }
    
    }
    

    If you see the above example the class is cohesive that means the variables are shared among the class to work together more variables are shared that means the class is highly cohesive and work as a single unit.

    0 讨论(0)
  • 2020-12-02 05:37

    An explanation of what it is from Steve McConnell's Code Complete:

    Cohesion refers to how closely all the routines in a class or all the code in a routine support a central purpose. Classes that contain strongly related functionality are described as having strong cohesion, and the heuristic goal is to make cohesion as strong as possible. Cohesion is a useful tool for managing complexity because the more code in a class supports a central purpose, the more easily your brain can remember everything the code does.

    Some way of achieving it from Uncle Bob's Clean Code:

    Classes should have a small number of instance variables. Each of the methods of a class should manipulate one or more of those variables. In general the more variables a method manipulates the more cohesive that method is to its class. A class in which each variable is used by each method is maximally cohesive.

    In general it is neither advisable nor possible to create such maximally cohesive classes; on the other hand, we would like cohesion to be high. When cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole.

    The notion of cohesion is strongly related with the notion of coupling; also, there is a principle based on the heuristic of high cohesion, named Single Responsibility Principle (the S from SOLID).

    0 讨论(0)
  • 2020-12-02 05:38

    High cohesion is a software engineering concept. Basically, it says a class should only do what it is supposed to do, and does it fully. Do not overload it with functions that it is not supposed to do, and whatever directly related to it should not appear in the code of some other class either.

    Example is quite subjective, since we also have to consider the scale. A simple program should not be too modularized or it will be fragmented; while a complex program may need more level of abstractions to take care of the complexity.

    e.g. Email class. It should contains data members to, from, cc, bcc, subject, body, and may contain these methods saveAsDraft(), send(), discardDraft(). But login() should not be here, since there are a number of email protocol, and should be implemented separately.

    0 讨论(0)
提交回复
热议问题