Lets say I have a function called do3() In order for that function to work I need the functions do1() and do2() be executed.
However, do1() and do2() could also be needed for other stuff (maybe for do4())
All these functions are public (and must be public).
Question, how should i implement the code?
Option 1 :
function do3() {
do2()
do whatever is needed for do3
}
function do2() {
do1()
do whatever is needed for do2
}
function do1() {
do whatever is needed for do1
}
So if i call do3(), i am sure that everything will be done, although coupling will appear
Option 2
function do3() {
do whatever is needed for do3
}
function do2() {
do whatever is needed for do2
}
function do2() {
do whatever is needed for do1
}
So when i want to call do3() i have to
do1()
do2()
do3()
I feel that the second option is better as it has less coupling, however I cannot really explain why, it is more like a feeling. I think that if I use option one and one day I change do2() I may have problems.
With option 2 however I must be sure to call do1 and do2 every time i want to use do3.
If someone has a better idea (option 3?) would be great to.
Thanks
"Lets say I have a function called do3() In order for that function to work I need the functions do1() and do2() be executed. "
Juan: As per your description do3() is dependent on do1() and do2(). The dependency Graph is
In this case you should go for the second approach.
If your dependency graph is:
ie
do3 depends on do2
do2 depends on do1
In this case you should go for first approach.