Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of parameters in method

I have Selenium test which fills a form. I have a method for it but this method has overgrown in terms of number of parameters -

 newMerchantPage.addEditMerchant(merchantDomain, merchantName,
            merchantCategory, true, merchantDescription, merchantNotes,
            merchantTags, true, true, false, false, merchantTitle,
            additionalDescription, merchantHeading, dummyCouponLink, true);

There are only Strings and boolean. I was thinking to use collection and then iterate through collection in called method to do some more processing. Though yet not sure if this is the way to go about. Any recommendations?

MODIFIED METHOD:

After implementing couple of sugggestions my method (of a different method) call looks like -

ContactPage contactPage = new ContactPage(driver); 
setContactFormData(); 
contactPage.setName(name).setEmailAddress(emailAddress).setSubject(subject).setM‌ ​essage(message); 
contactPage.submitContactForm(contactPage); 

submitContactForm in turn calls different utility methods. How bad does it look? Especially the last line (method call on object and same object being passed as argument) ?

like image 677
Tarun Avatar asked Feb 23 '23 23:02

Tarun


1 Answers

One common approach is to wrap the parameters in a class. This class could then provide set-methods which return this to allow for a nice chaining. (See ProcessBuilder for a good example.)

Example:

MerchantData data = new MerchantData();   // initialize with sensible defaults

data.setDomain("example.com")
    .setName("Some Name")
    .setTags("tag1, tag2);

newMerchantPage.addEditMerchant(data);
like image 159
aioobe Avatar answered Feb 25 '23 17:02

aioobe