5.2 Parameter Lists
A subprogram or entry parameter list is the interface to the abstraction implemented by the subprogram or entry. It is important that it is clear and that it is expressed in a consistent style. Careful decisions about formal parameter naming and ordering can make the purpose of the subprogram easier to understand, which can make it easier to use.
Formal Parameters
guideline
- Name formal parameters descriptively to reduce the need for comments.
example
List_Manager.Insert (Element => New_Employee, Into_List => Probationary_Employees, At_Position => 1);
rationale
Following the variable naming guidelines ( 3.2.1 and 3.2.3 ) for formal parameters can make calls to subprograms read more like regular prose, as shown in the example above, where no comments are necessary. Descriptive names of this sort can also make the code in the body of the subprogram more clear.
Named Association
guideline
- Use named parameter association in calls of infrequently used subprograms or entries with many formal parameters.
- Use named association when instantiating generics.
- Use named association for clarification when the actual parameter is any literal or expression.
- Use named association when supplying a nondefault value to an optional parameter.
instantiation
- Use named parameter association in calls of subprograms or entries called from less than five places in a single source file or with more than two formal parameters.
example
Encode_Telemetry_Packet (Source => Power_Electronics, Content => Temperature, Value => Read_Temperature_Sensor(Power_Electronics), Time => Current_Time, Sequence => Next_Packet_ID, Vehicle => This_Spacecraft, Primary_Module => True);
rationale
Calls of infrequently used subprograms or entries with many formal parameters can be difficult to understand without referring to the subprogram or entry code. Named parameter association can make these calls more readable.
When the formal parameters have been named appropriately, it is easier to determine exactly what purpose the subprogram serves without looking at its code. This reduces the need for named constants that exist solely to make calls more readable. It also allows variables used as actual parameters to be given names indicating what they are without regard to why they are being passed in a call. An actual parameter, which is an expression rather than a variable, cannot be named otherwise.
Named association allows subprograms to have new parameters inserted with minimal ramifications to existing calls.
notes
The judgment of when named parameter association improves readability is subjective. Certainly, simple or familiar subprograms, such as a swap routine or a sine function, do not require the extra clarification of named association in the procedure call.
caution
A consequence of named parameter association is that the formal parameter names may not be changed without modifying the text of each call.
Default Parameters
guideline
- Provide default parameters to allow for occasional, special use of widely used subprograms or entries.
- Place default parameters at the end of the formal parameter list.
- Consider providing default values to new parameters added to an existing subprogram.