jian ye
jian ye

Reputation: 1

how to organize the code when multi varibales get data form multi sources?

I have an API that return a struct which contains variables: a, b and c. To get the value of a,b,c, I have to get original data from multi services,e.g. , S1,S2. S1 returns a1,b1,and S2 returns b2,c2 Now I have to calculate the value of a using a1 and c using c2 and b using b1 and b2. So in this case, how to organize the code? e.g.:

ReqS1();    // here to calculate a and store b1
ReqS2();    // here to calculate b and c

or

ReqS1();  // here to store a1 and b1
ReqS2();  // here to store b2 and c2
CalculateA();  // here to calculate a
CalculateB();  // here to calculate b
CalculateC();  // here to calculate c

or any other better ways?

Upvotes: 0

Views: 38

Answers (1)

Ali Ibrahim
Ali Ibrahim

Reputation: 119

Your design decision would depend on if you have control over the services, if so change s1 for example to return a1 & c2, and s2 to return b1& b2 then do the follwing:

ReqS1();  // here to store a1 and b1
CalculateA();  // here to calculate a
CalculateC();  // here to calculate c
ReqS2();  // here to store b2 and c2
CalculateB();  // here to calculate b

otherwise you should go with the second approach, since the calculation of a for example would be useless if for example ReqS2() failed to retrieve b2 and c2, so you will not return the new struct that holds a,b and c.

Upvotes: 2

Related Questions