introduction to connascence
TRANSCRIPT
ConnascenceViernes técnico 25/11/2016
Definición lingüística
Definición técnica
Connascence ⇝ Acoplamiento
Conjunto de patrones, “métricas” para poder razonar mejor cómo de acoplado está tu
código (no sólo hablar en base a “sensaciones”).
Ofrece un vocabulario común para hablar sobre los diferentes tipos de acoplamiento.
Dos entes son “connascents” cuando deben cambiar de manera conjunta para que el
sistema siga siendo válido: Si A cambia, entonces B debe cambiar.
Tipos de connascence● Estático
○ CoN: de nombre
○ CoT: de tipo
○ CoM / CoC: de significado o convención
○ CoP: de posición
○ CoA: de algoritmo
● Dinámico
○ CoE: de ejecución
○ CoT: de tiempos
○ CoV: de valores
○ CoI: de identidad
Properties
● Strength
● Locality
● Degree
Static Connascence
Connascence of Name (CoN)Multiple components must agree on the name of an entity.
Method names are an example of this form of connascence: if the name of a method changes, callers of that
method must be changed to use the new name.
Connascence of Type (CoT)Connascence of type is when multiple components must agree on the type of an entity.
Static languages: the compiler does your job.
Dynamic languages: you’re f** up
Connascence of Meaning (CoM)Connascence of meaning is when multiple components must agree on the interpretation of data values.
Basically: magic number, magic strings, null/None, booleans, etc.
Solution: use of constants, Enums, etc.
Connascence of Position (CoP)Connascence of position is when multiple entities must agree on the order of values.
Example 1: positional parameters in functions/methods
Solution: named parameters or an object
Connascence of Position (II)Example 2: returning tuples or lists
Solution: object or dictionary/map
Connascence of Algorithm (CoA)Connascence of algorithm is when multiple components must agree on a particular algorithm (on how to
process something).
● Transmisión de datos
○ Emisor y receptor deben consensuarlo
● Validaciones
○ Validación del email (JS, Controller, Model): en varios sitios, ¿de varias maneras?
● Codificaciones/Encriptaciones
● Algoritmos de compresión
● Testing:
○ Bad smell: verificar que un algoritmo funciona según lo esperado (se duplica en el test)
Dynamic Connascence
Connascence of Execution (CoE)The order of execution of multiple components is important.
Example:
● objects encapsulating state machine
Connascence of Time (CoT)The timing of the execution of multiple components is important.
Examples:
● Multithreading
● race conditions
Connascence of Time (II)
Java 1.5: AtomicInteger, e.g. para contadores accedidos por varios threads
(lectura + escritura no separada)
Connascence of Value (CoV)Several values must change together (a.k.a the values of two components are related).
It’s a runtime problem.
Ejemplo: http://connascence.io/value.html
Connascence of Value (CoV)
Connascence of Identity (CoI)Two components must reference the same entity/object.
Ejemplo en nuestros tests: mismo repo para leer y escribir.
Properties / Axes of Connescence
Properties
● Strength
● Locality
● Degree
Axe: StrengthThe strength of a form of connascence is determined by the ease with which that type of coupling can be
refactored.
Stronger connascences are harder to discover or harder to refactor.
For example, connascence of name is a weak form of connascence because renaming entities across a codebase
is usually reasonably trivial. However, connascence of meaning is considered a stronger form of connascence
since semantic meaning is harder to find across an entire codebase.
Static connascences are considered to be weaker than dynamic connascences, since static connascences can be
determined simply by examining the source code.
Axe: LocalityThe locality of an instance of connascence is how close the two entities are to each other.
Code that is close together (in the same module, class, or function) should typically have more, and higher
forms of connascence than code that is far apart (in separate modules, or even codebases).
Axe: DegreeAn entity which is connascent with thousands of other entities is likely to be a larger issue than one is
connascent with only a few.
Connascence might be acceptable in limited degree but unacceptable in large degree.
Recursos
● https://en.wikipedia.org/wiki/Connascence_(computer_programming)
● http://connascence.io/
● Connascence en Python: https://www.youtube.com/watch?v=iwADIlIgDNA
● Connascence Examined: https://www.youtube.com/watch?v=HQXVKHoUQxY
● How to measure coupling: https://www.youtube.com/watch?v=L727roWRfFg