devxlogo

Use a Struct Instead of a Long Argument List

Use a Struct Instead of a Long Argument List

Functions having a long list of arguments such as:

 void retrieve(const string& title, const string& author, int ISBN,  int year, bool&  inStore); 

can become a maintenance problem, since their argument list is likely to be changed in the future. For example, a URL with a book cover image may be added or other arguments may be omitted. Consequently, every occurrence of a function call has to be modified appropriately. A better solution is to pack the entire argument list in a single struct and pass it by reference (or by address in C) to the function:

 struct Item{string title;string author; int ISBN;  int year, bool  inStore };void retrieve(Item& book);

The advantages of this technique are obvious:

1. Maintenance. Every modification will be localized to the definition of the struct, but no other code modifications will be required.
2. Performance. Since a reference is all that has to be passed to the called function, you may gain a slight performance boost.
3. Future extensibility. A struct can serve as a base for other classes. It may be extended to have member functions as well.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist