Problems with parametric design

Source: Internet
Author: User
I find that sometimes programmers do not write code, but are always tangled in the code how to write it better, good extension, and beautiful, a code control multiple points. After all, programmers are the words like gold, less dozen lines is a line, concise one is one.

Although the accumulation of more and more experience does not have to tangle more and more, but today back to an old problem: The method of the parameter table design to a good, or an array by the key name to express good?

I think we all know this problem, will always come into contact with this, the parameters of the table was set ABCDE parameters, later requirements change, BC Delete, increase F, the whole project to find code to change, but also afraid to change the leak where, leading to the old code to pass many parameters and run error
I found that the JS aspect is more and more popular with the key value pair (it is associative array bar) to do the argument, especially some UI plug-in initialization of the specified options, of course, because of those parameters too many, with one parameter to pass the word is difficult to remember which parameter bit is which, If you use key-value pairs, you only need to remember which keys are necessary to control the key and can choose to control the properties of the key can be, to a certain extent, this design is for optional parameters, and PHP occasionally see such a design, but in fact, it seems that the method call will be troublesome, and express not directly
Addusericon (User ID, number of coins);
It's always like Addusericon (Array (' UID ' + User ID, ' qty ' = number of coins))

Everybody talk about it, above is just a simple example, complex and feel the array to do a point, so that the east and West will become not unified, the development process will be entangled in the problem of the code is not unified, a practice such a practice. Of course, the only thing you can do.


Reply to discussion (solution)

For C + + your problem is implemented through the overloads of the function
But PHP doesn't support overloading (because Web development doesn't have to be that complicated), but PHP provides the default for parameters to work around
So you can determine the direction of the process by checking the actual number of incoming

But it's not advisable for you to change the parameters from ABCDE to Adef.
Because the parts that were called by ABCDE will be ambiguous,

For C + + your problem is implemented through the overloads of the function
But PHP doesn't support overloading (because Web development doesn't have to be that complicated), but PHP provides the default for parameters to work around
So you can determine the direction of the process by checking the actual number of incoming

But it's not advisable for you to change the parameters from ABCDE to Adef.
Because the parts that were called by ABCDE will be ambiguous,

In other words, for example, the user's age, gender, region, xx,yy ..., but one day the project needs a change and said no gender and region, then add a signature it ...
Suppose the method is Setuserinfo
Then set the user information This call point may be in the user data modification, background to the foreground user data modification, or the user through other interface operation modification part of the information call, now if not to change the method parameter table, what should I do? Open multiple new methods, SetUserInfo2? But the other call point is to modify the parameter name and the argument table of the call.

Generally does not involve the details of the data collection
For example, user information is a collection of {User ID, age, gender, region, etc.}, and the user ID uniquely identifies the user
Another example is {age, gender, region} is a filter set, and {age, signature} is another filter set
So all you need to do is pass the filter set number you need to make the program generic.
And the technique of maintaining these collections is: Data dictionary

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.