METHODS:MULTIPLE PARAMETERS/STRUCTURE?

Nigel Bunyan

Total Post:20

Points:140
Posted by  Nigel Bunyan
 303  View(s)
Ratings:
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]
  1. Tarun Kumar

    Post:214

    Points:1642
    Re: Methods:Multiple parameters/structure?

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!