Home > DeveloperSection > Forums > Methods:Multiple parameters/structure?
Nigel Bunyan
Nigel Bunyan

Total Post:20

Points:140
Posted on    December-10-2015 11:27 PM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 150  View(s)
Rate this:

I have notice by looking sample code in iOS, the methods design structures instead of multiple parameters.
Why is that? As far as ease of use, I personally prefer the latter, but as far as performance goes, is there one better choice than the other?

[pencilObj drawPoint:Point3Make(7,9,20)]
[pencilObj drawPointAtX:7 Y:15 Z:25]


Tarun Kumar

Total Post:210

Points:1614
Posted on    December-13-2015 11:09 PM

Here are following guidelines to decide parameter structure (apple does not recommend or prefer passing multiple parameters vs a structure/object):
- Use individuals parameters or objects when it makes sense in the particular scenario.
- Pass objects/structures when it makes sense for the method to understand many/all members of the object.
- Pass objects/structures when you want to validate some rules on the relationship between the various members of the object. This allows you to ensure the consumer of your method constructs a valid object prior to calling your method (thus eliminating the need of the method to validate these conditions).
- Pass individual arguments when it is clear the method makes sense and only needs certain elements rather than the entire object
- Using a variation on your example, a paint method that takes two coordinates (X and Y) would benefit from taking a Point object rather than two variables, X and Y.
- A method retrieveOrderByIdAndName would best be designed by taking the single id and name parameter rather than some container object.

Don't want to miss updates? Please click the below button!

Follow MindStick