Should/does loose coupling also be applied between methods of the same class?

StackOverflow https://stackoverflow.com/questions/11233298

  •  17-06-2021
  •  | 
  •  

Question

Assume class A with methods M1 and M2 has low coupling with other classes

a) Should we also make sure that each individual method in class A is not tightly coupled with any other method in the same class? Thus, should we make sure that changing code in A.M1 doesn't also require us to change code in A.M2?

b) I assume if A.M1 is performing two closely related tasks T1 and T2 instead of just a single task, then T1 and T2 are tightly coupled, since changes in T1 may also require changes in T2?

thank you

Was it helpful?

Solution

Write code that follows single-point-of-maintenance. If you change something, only change it in one place. This will reduce bugs throughout your code. That being said avoid code duplication and split classes, methods, namespaces, etc. into parts parts with a single responsibility.

Changing something in method A() should not force you to make a change in method B(). Maybe use a helper function in both that shares common functionality.

EDIT: The SOLID acronym is a good one to follow for software design/engineering: http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top