Connascences that can be found by visually studying the code are static connascences.
Connascence of name is when two or more things must agree on the name of a component or entity. For example, if the name of a function changes, all the code that uses that function must change the name of the function, too.
The following code snippet is from the Python standard library. Changing the class name from Request
, method name from set_proxy
, or parameter names from headers
will require changing these names elsewhere in the code.
class Request: def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None): pass def set_proxy(self, host, type): pass
Connascence of type is when two or more things must agree on the type of something.
The following example contains a C++ code snippet. The area()
function accepts two parameters of type integer and returns an integer value. The variable ar
must be of type integer as it holds the return value. Writing string ar
instead of int ar
will produce an error.
#include <iostream> using namespace std; int area ( int breadth, int height) int main() { int ar = area(4,5); }
Connascence of meaning occurs when components must agree on the meaning of particular values. Connascence of meaning often occurs when we use magic numbers, a specific value that has meaning and is used in multiple places.
The following code illustrates that if name
is Admin
, then an admit()
function is called, which gives access to some privileges. All the code in the program must agree that the name Admin
is used for gaining those privileges.
#include <iostream> using namespace std; int switchfunc(string name) { if (name == 'Admin') { admit() } else { return 0; } } int main() { string name = 'Admin' return 0;
Connascence of position occurs when code in two different places must agree on the position of things. This scenario most commonly occurs in parameter lists where the order of parameters in a method’s parameter list must remain the same in all uses of that method. If you add a parameter to the middle of an existing parameter list, all uses of that method must add the new parameter in the correct position.
Consider the following definition of the function getDate()
. All the code that uses this function must agree that the parameters day, month, and year will be stated in that order.
def getDate(day, month, year)
Connascence of algorithm occurs when two modules must agree on a specific algorithm to function together.
Consider a system where data is being sent from one service to another and the information being transferred is sensitive and must be encrypted. The services are coupled by connascence of algorithm because both must agree on the encryption algorithm that will be used. If one service changes the encryption algorithm, the other service must change to the same algorithm.
The following four levels of connascence are said to be dynamic because they can only be discovered by running your code. These levels are stronger than the static ones because they only reveal themselves at runtime, making them hard to spot and often harder to fix.
Connascence of execution occurs when the order of code execution is required for the system to be correct.
Here is a simple example to demonstrate connascence of execution:
UserRecord.FirstName := 'Alicia';
UserRecord.LastName := 'Florrick';
UserRecord.Age = 47;
UserManager.AddUser(UserRecord);
UserRecord.Birthday := EncodeDate(1968, 12, 3);
This code adds the Birthday value after the user has been added. Upon examining the code, it’s clear it won’t work, but you can imagine a more complex scenario that is harder to spot
Connascence of timing occurs when the timing of execution makes a difference in the outcome of the application. The most obvious example of this is a threaded race condition, where two threads pursue the same resource, and only one of the threads can win the race.
Connascence of identity occurs when two components must refer to the same object. If the two modules refer to the same thing and then one changes the reference, the other object must change to the same reference. It’s often subtle and difficult to detect connascence of identity; as a result, this is the most complex form of connascence.
RELATED TAGS
CONTRIBUTOR
View all Courses