Cohesion

There are four types of cohesion that are beneficial to Visual Basic components:

> Functional Cohesion. Functional cohesion creates the tightest binding within a component. A component that has functional cohesion only does one thing. If a component performed more than one function, than the component would be doing two separate things, which means the component is not tightly bound. All of your Visual Basic methods, both private and public, should have functional cohesion; that is, they should only do one thing. This makes it very clear what the function does and makes it easy to debug.

> Sequential Cohesion. A component that has sequential cohesion will have internal components that must work in a specific order. The internal components will also share internal data. Several internal methods that pass data from method to method through parameters, and work in a specific order, would be considered sequential cohesion. This is common when you create functions or subs in your Visual Basic code that perform some operations on the data. Sequential cohesion makes it easy to follow the flow of the code. Debugging is easier as you can check the parameters which are passed into a method 'by reference'; this means that a variable itself (rather than its value) is passed into the method, so the value of the variable can be changed within the procedure.

> Temporal Cohesion. Temporal cohesion means that internal operations are combined so that they can be done at the same time. For example, when you update an order, you will update the Orders, Order Details and Products tables in the database at the same time.

Components with these types of cohesion are tightly bound, perform a clear set of services and are easy to debug and understand.

> Communicational Cohesion. In communicational cohesion, the internal operations of the component share the same data but do not communicate with each other. For example, if you create a global connection variable that is used independently by all of your methods, this would be communicational cohesion. You must be careful with global variables, though, as these are not always the best coding technique for Visual Basic. You should be very careful when working with communicational cohesion always to set and retrieve the values of global variables through methods and properties (rather than accessing them directly), even when the variables are only used privately.

All of these types of cohesion result in Visual Basic code that is easy to read and debug. Because components with these types of cohesion are tightly bound, they can easily be tested in isolation from the rest of the project. This means that if an error is found during the testing of an individual component, the error can only be in that component. If each function has a single clear purpose, it should be fairly easy to see what is wrong and find the function in which the error occurred.

On the other hand, there are three other types of cohesion that result in Visual Basic code that is poorly written. These are:

> Procedural Cohesion. This is what happens when a series of operations are grouped together because they occur in a specific order. This differs from sequential order in that the operations do not share data. When two routines manipulate different data, they must be performing two completely different functions, so they are very loosely cohesive. There will be no clear logic in the order of the operations and consequently it will be difficult to follow the code. As functions are grouped together without having logical cohesion, it becomes difficult, if not impossible, to test individual functions and components. Often, entire sections of code must be tested at once. If a bug is found, you must search through mountains of code to find it, which could take a very long time.

> Coincidental Cohesion: This is simply a worse case of procedural cohesion. In procedural cohesion, there is at least some logic in the order of the procedures, but there is no sharing of data. In coincidental cohesion, there is simply no reason for the order of the code. Code jumps from one module to the next with no obvious pattern or reason. The common term for this is 'spaghetti code'. It is unreadable, unmaintainable and impossible to debug. Visual Basic has evolved to a level where the developers should be professionals. Professional developers should never be writing this type of code. It does not matter how many great features of Visual Basic a developer may know or how deep their knowledge of the language is if they write code that is a tangled, unmanageable mess.

> Logical Cohesion: The routines have a logical parameterpassed in that determines what the routine will do. Inside the routine will be a huge Select Case (or, even worse, an If ... ElseIf statement) that will determine what the routine will do. These routines can stretch to hundreds of lines of codes and become completely unmanageable.

The most common reason for procedural and coincidental cohesion is a total lack of a design phase. During the design phase, and specifically the physical stage, you can see the big picture and organize your components so that there is a logical flow of the internal components. Being given a set of properties and methods that your component is supposed to have, and then just start hacking out code, will result in code that does not have a clear organization. Sometimes a very carefully designed project can end up with spaghetti code because of feature creep. As new features are added in the middle of coding, modules have to be hacked and changed. New modules suddenly need to be stuck in to add this new functionality and all order is soon lost. This is why it is so essential to create a good design and get all team members, especially the stakeholders and sponsors, to sign off on the final design. A clear agreement has to be made that once the design is final, new features will be added in the next cycle. Because there is a documented design, even new team members will be able to work within the existing design and to add features in ways that continue "clean" design in the next cycle.

365 Days Of Motivation

365 Days Of Motivation

Stop Wasting Time And Learn How To Stay Motivated. Finally! Discover How To Stop Your Mind From Wandering, And Upgrade Your Motivation. You Can Hack Your Motivation Levels, Allowing You To Take Your Life To The Next Level.

Get My Free Ebook


Responses

  • genet
    What is cohesion in project management?
    1 month ago

Post a comment