The relationship between public functions

Suppose I have a function called do3 (). In order for this function to work, I need the functions do1 () and do2 ().

However, do1 () and do2 () may also be needed for other things (perhaps for do4 ())

All of these features are publicly available (and must be publicly accessible).

Question: how do 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 the connection will be displayed

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, since it has less grip, but I can’t explain why, it is more like a feeling. I think that if I use the option one and one day, I change do2 (), I may have problems.

With option 2, however, I must definitely call do1 and do2 every time I want to use do3.

If anyone has a better idea (option 3?), It would be great.

thanks

+4
source share
3 answers

"Lets say that I have a function called do3 (). For this function to work, I need the functions do1 () and do2 ()."

Juan: according to your description do3 () depends on do1 () and do2 (). Dependency graph

  - ->do2() do3() - ->do1() 

In this case, you should go for the second approach.

If your dependency graph:

 do3()- ->do2() - -> do1() 

ie

  • do3 depends on do2

  • do2 depends on do1

In this case, you should go for the first approach.

 --> : shows the dependency. 
0
source

Binding is a concept associated with classes that are not functions. A function must be able to call any other function of the same class in which it lives. There is no communication problem.

Your first option is fine, there is nothing wrong with a do3 call to do2 and do2 calling do1 as long as they are all in the same class.

You should not choose option 2, as you will need to repeat the code everywhere.

+1
source

The short answer is that if do3 () should always make a do2 / do1 call, and there are no contexts where the caller might need to perform some action between these calls, then do2 really needs to be included in do3, etc. , I would also argue that if doX calls are not part of an API or other difficult-to-change environment, it would be wise to avoid splitting calls “just in case”, in the future there will be some kind of event that needs to be split (the principle of careful design).

Longer answer: One way to verify the truth is to investigate pathological cases. Taking your second option to the extreme entails completely unraveling the functional composition to the goal of completely eliminating the functions; in the end, some function calls do1 () do2 () do3 () and is thus “connected” to these functions.

[soap box] This is simply not a true statement that static dependencies (traction) are necessarily a vice, although this concept is now popular. Static dependencies may seem inflexible, but they are also easy to understand, machine-verifiable, and highly optimized. To illustrate this point, consider this hypothetical code:

 person = WebRequest('/GetPerson'); if (person.Phone.AreaCode = '') person.Phone.AreaCode = GetAreaCodeFromZip(person.Zip); ... 

Logic like this can often be decomposed for many reasons:

 requestService = CreationFactory(IRequest); requestService.Configure(ConfigurationService.GetConfiguration(requestService)); requestService.SetEntityContext('Person'); response = requestService.Invoke(); entity = EntityService.ProcessEntity(response.Data); EntityService.RegisterEntityCorrectionService(entity, IAreaCorrectionService); ... interface IAreaCorrectionService ... class AreaCorrectionService : IAreaCorrectionService ... ServiceFactory.Register(AreaCorrectionService... 

My point is that there are costs in terms of performance, readability, and even reducing declarativeness for "decoupling." This is rarely seen explicitly when considering inversion of control and other frameworks.

0
source

All Articles