Video in progress
Explain connascence. What does it add about coupling beyond "low coupling, high cohesion"?
Connascence means two components are related so that if you change one, you must change the other. It splits coupling into kinds (name, type, position, meaning, algorithm, timing...) and says exactly which is stronger and how it relates to distance.
📖 Answer
"Keep coupling low" is right, but which coupling? Connascence gives you a vocabulary. From weak to strong:
Static (visible by reading the code):
- Name: knowing a method's name. The weakest, solved by an IDE rename.
- Type: knowing a parameter's type.
- Meaning:
status == 3means "canceled". Magic values. - Position:
new User("Ali", "Valiyev", 25, true): the order matters and it's easy to get wrong. - Algorithm: two places must use the same hash or signature algorithm.
Dynamic (at runtime):
- Execution order:
init()must be called beforestart(). - Timing: two operations must happen within a certain time.
- Value: two values must change together (a cache and the DB in two places).
- Identity: two components must refer to exactly the same object.
Three rules:
- Turn strong connascence into weaker. Meaning -> Type: an
enum Statusinstead ofint status. Position -> Name: a builder or a record's named fields. - The greater the distance, the weaker it should be. Position connascence inside one method is fine; between two services it's a disaster.
- Degree: how many elements are involved. The order of three arguments is tolerable; of twelve, it isn't.
A cross-service example: two services must calculate an HMAC signature identically (Connascence of Algorithm). To reduce it, move the signature logic into one small versioned library or a standard (JWS). Two services must know the same status numbers (Meaning): replace them in the contract with named enum strings.
Sign up to mark studied questions and save your progress.