img wangjiong

High-Quality Routines(2)

发表于2004/9/23 14:47:00  533人阅读

How to Use Routine Parameters
1. Put parameters in input-modify-output order
Instead of ordering parameters randomly or alphabetically, list the parameters that are input-only first, input-and-output second, and output-only third.
2. If several routines use similar parameters, put the similar parameters in a consistent order.
3. Use all the parameters.
If you pass a parameter to a routine, use it. If you aren't using it, remove the parameter from the routine interface.
4. Don't use routine parameters as working variables.
5. Document interface assumptions about parameters.
If you assume the data being passed to your routine has certain characteristics, document the assumptions as you make them.
The kind of interface assumptions about parameters we should document:
    Whether parameters are input-only, modified, or output-only.
    Units of numeric parameters(inches, feet,meters and so on)
    Meaning of status codes and error values if enumerated type aren't used.
    Rangers of expected values.
    Specific values that shoudl never appear.
6. Limit the number of a routine's parameters to about seven.
7. Consider an input, modify, and output naming convention for parameters.
8. Pass the variables or objects that the routine needs to maintain it's interface abstraction.
If a routine need 3 paramethers that include in an object, pass the 3 parameters or the entire object?
To solve the problem, think that what abstraction is presented by the routine's interface?
    If the abstraction is that the routine expects you to have 3 specific data elements, and it is only a coincidence that those 3 elements happend to be provided by the same object, then you should pass the 3 specific data individually.
    If the abstraction is that you will always have that particular object in hand and the routine will do something or other with that object, then you truly do break the abstraction when you expose the 3 specific data elements.
9. Don't assume anything about the parameter-passing mechanism.
10. Make sure actual parameters match formal parameters.
0 0



取 消