Introduction to Connascence

27
Connascence Viernes técnico 25/11/2016

Transcript of Introduction to Connascence

Page 1: Introduction to Connascence

ConnascenceViernes técnico 25/11/2016

Page 2: Introduction to Connascence

Definición lingüística

Page 3: Introduction to Connascence

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.

Page 4: Introduction to Connascence

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

Page 5: Introduction to Connascence

Properties

● Strength

● Locality

● Degree

Page 6: Introduction to Connascence

Static Connascence

Page 7: Introduction to 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.

Page 8: Introduction to Connascence

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

Page 9: Introduction to Connascence

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.

Page 10: Introduction to Connascence

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

Page 11: Introduction to Connascence

Connascence of Position (II)Example 2: returning tuples or lists

Solution: object or dictionary/map

Page 12: Introduction to Connascence

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)

Page 13: Introduction to Connascence

Dynamic Connascence

Page 14: Introduction to Connascence

Connascence of Execution (CoE)The order of execution of multiple components is important.

Example:

● objects encapsulating state machine

Page 15: Introduction to Connascence

Connascence of Time (CoT)The timing of the execution of multiple components is important.

Examples:

● Multithreading

● race conditions

Page 16: Introduction to Connascence

Connascence of Time (II)

Java 1.5: AtomicInteger, e.g. para contadores accedidos por varios threads

(lectura + escritura no separada)

Page 17: Introduction to Connascence

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

Page 18: Introduction to Connascence

Connascence of Value (CoV)

Page 19: Introduction to Connascence

Connascence of Identity (CoI)Two components must reference the same entity/object.

Ejemplo en nuestros tests: mismo repo para leer y escribir.

Page 20: Introduction to Connascence

Properties / Axes of Connescence

Page 21: Introduction to Connascence

Properties

● Strength

● Locality

● Degree

Page 22: Introduction to Connascence

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.

Page 23: Introduction to Connascence

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).

Page 24: Introduction to Connascence

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.

Page 25: Introduction to Connascence

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

Page 26: Introduction to Connascence
Page 27: Introduction to Connascence