The problem of parameter design I found that sometimes programmers don't want to write code, but they are always struggling with how to write this code to make it better, better scalable, and more beautiful. one piece of code controls multiple points. after all, programmers are fond of words like gold. one line is less than one line, and the other is a concise one.
Although more and more experiences are accumulated, there is no need to worry about it, but today we are back to an old question: is the parameter table of the method well designed to be one by one, or is it better to express an array through a key name?
I think everyone knows this problem and will always be exposed to this problem. The parameter table originally sets the ABCDE parameter. later, the requirement is changed, BC is deleted, and F is added. you can change the code for the entire project, I am also afraid that I may miss it. as a result, the old code fails to pass less parameters and the code runs incorrectly.
I found that JavaScript is becoming increasingly popular with key-value pairs (join an array) for parameter passing, especially the specified options during UI plug-in Initialization. of course, because there are too many parameters, if you use a key-value pair, you only need to remember the key to be controlled and the property key to be controlled, to a certain extent, this design is intended for optional parameters, but PHP occasionally sees such a design. However, in fact, it seems that method calling will be troublesome and cannot be expressed directly.
AddUserIcon (user ID, number of gold coins );
It's like addUserIcon (array ('uid' => User ID, 'qty '=> Number of coins ))
Let's talk about it. the above is just a simple example. the array is complicated and easy to handle. in this way, the entire East and West are not uniform, and the code inconsistency will be entangled in the development process, this is one practice. of course, you are only angry with yourself. the boss only needs you to implement it.
Reply to discussion (solution)
Your problem with C ++ is implemented through function overloading.
However, php does not support overload (because web development does not need to be complicated), but php provides default parameter flexibility.
Therefore, you can check the actual number of incoming data to determine the process trend.
However, changing the parameter from ABCDE to ADEF is not advisable.
Because the original parts called by ABCDE will be ambiguous
Your problem with C ++ is implemented through function overloading.
However, php does not support overload (because web development does not need to be complicated), but php provides default parameter flexibility.
Therefore, you can check the actual number of incoming data to determine the process trend.
However, changing the parameter from ABCDE to ADEF is not advisable.
Because the original parts called by ABCDE will be ambiguous
Another example is the age, gender, region, XX, YY ..., but one day, the project needs to change and says no gender or region, and then add a signature...
Assume the method is setUserInfo.
The call point for setting user information may be modified in user data, and the frontend user information may be modified in the background, or some information may be modified by the user through other interfaces, what should I do if I do not change the parameter table of this method? Open multiple new methods, setUserInfo2? However, you need to modify the parameter name and parameter table of the call at other call points.
Generally, the data set details are not involved.
For example, the user information is a collection of {User ID, age, gender, region, and so on}, and the user ID can uniquely identify the user.
For example, {age, gender, region} is a filtering set, while {age, signature} is another filtering set.
Therefore, you only need to pass the desired filter set number to make the program generic.
The technology for maintaining these sets is data dictionary.