Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Sep 10, 1998

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.

Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date