Contradiction in software development - a simple example

zhaozj2021-02-17  51

In the previous article, I have mentioned that the software development is full of contradictions. Some principles themselves are contradictory, and they need to be confused and balanced in these contradictions. Here is a simple example from the actual actual, I hope to give you a little revelation, just don't know if it is appropriate (explanation: the program C language description, T is each data type).

When defining an interface of a class, you need to define the Getter / Setter function of both correlation variables A and B. In order to make the interface as thin as possible, we use the first method to use a pair of getter / setter:

Getab (t * pa, t * pb); setab (t a, t b);

However, if you only want to access one of the variables, call getab / setab needs additional work, and the CLIENT code will be very cumbersome.

In order to obtain the value of A, an additional variable B: T A, B; GetAb (& A, & B); COUT << a; // only get a

In order to set a value of A, the value of B is retained, and the getab: t a, b; Getab (& A, & B); a = 10; setab (a, b); // ONLY SET A

To this end, we use the second method to remove it into two pairs of functions:

T geta (); t getb (); void seta (t a); setb (t b);

In this way, the interface is doubled. However, things did not end this. Sometimes, like getab / setab does not play only the role of simplified interfaces. When calling Setab, we can examine the legitimacy of A and B, such as: A, b represent the upper and lower limits of a value domain, then assume that if A> B is unreasonable. It should be refused. This legitimacy check is not so smoothly used in the second method, and the thick looks should be as follows:

Void seta (t a) {if (a> m_b) return; // error else m_a = a;}

Void setb (t b) {IF (b

If you just set a value in A, B, there will be no trouble, this method is completely competent. But if you are set up? Do not consider the initial value of A and B, such as the settings of A and B, such as the settings of A and B, etc., and try to reset A, B is 15, 20, and the problem is generated. :

Seta (15); setb (20);

The result became a = 5, b = 20 (Completely Error).

If you want to get the correct result, you need to reverse the order of calling SETA and SETB. However, if A, B is set to 1, 6, respectively? That is, in order to ensure successful settings, the Client code needs very careful, different situations, and uses different calls. For the above case, it is difficult to do the correct legitimacy check with the second method, because the function of Signature determines that it cannot know the associated another value, so that it cannot be properly judged.

Therefore, the evolution process of things is: in order to streamline the interface, we choose method one; in order not to increase the extra customer code, we choose method two; in order to conduct legality check, we have to choose the method one. There have been two methods, three principles ("for ..."). And when we finally decide to choose a method, it is still possible to carry a "increased additional customer code". And if you don't want to do this, maybe you will use both methods, that is, the Geta / Seta, Getb / Setb, GetAb / Setab is all defined as an interface. However, you may be accused "Interface Chaos", and for seta / setb, legitimacy check is still a problem.

Conclusion: When there are many ways to choose, there are different principles (selection basis), and we need to make a decision for actual situations. This decision often does not meet all principles, but it generally should be the maximum suitable for most situations. If the actual situation is not enough to make you a very affirmative judgment, then I am afraid that only habits and intuition can affect your decision. Just, maybe you will also modify your decision.

PS: For convenience, an extremely simple example is selected here. Frank, there may be exaggerated. In practice, for such a "getter / setter" problem, most of you will not be in such a difficult situation as the text, unless you are a perfectionist. You can make a decision soon, because even if you can't meet certain principles, the price will not be very high. Otherwise, those software developers don't have to write a few lines of code every day, and it will be deeply in the pain of contradiction. However, there is contradiction in such a fine place, you can imagine that "contradiction" is used to describe the software development process, which may not be exaggerated.

转载请注明原文地址:https://www.9cbs.com/read-29163.html

New Post(0)