How many parameters is too many?

Started by
10 comments, last by n0ob 20 years, 6 months ago
" Well, rather, how much parameter data is too much to"
" pass into a function for performance concerns? "

There are 3 considerations you need to look at: 1) how many times this function will be called, 2) how the parameters are used internally, and 3) how the data is organized externally.

1) Functions called in a tight loop need as little parameters as possible. Some other functions might be generalizations (like SQL calls) or open-ended such as printf(...) for which it is expected to have a long list of parameters. But generally, the less parameters you pass in the better.

2) Some of those parameters will be used as constants other will be used dynamically during the execution of the function. For example, you might have an int that specifies which algo to use or some mode your function needs to take into account while a floating point parameter will be modified during execution. Things that have the tendency to stay constant over a large number of uses should be put in a struct and passed by pointer. Also, if the function calls other functions in cascade, then putting parameters in a struct you pass around by pointer is a better solution than having to expand them canonically in formal formal parameters for each sub-function.

3) If the data you pass in to a function comes from structured storage areas, then you should pass pointer to those instead. Unless of course you can''t make any assumption about the app''s data organization because you''re building a library.

Hope this helps.
Advertisement
If you see a group of functions in which you often pass the same arguments, it''s an indication that you could probably turn them into a class that has these parameters as member variables (of course, don''t do it unless these functions show to belong together logically)

This topic is closed to new replies.

Advertisement