SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    question regarding arguments vs. more methods

    Hi,

    I often end up with methods taking more than... three arguments. I know it's a call for refactoring.

    My question, though, is, how do you make sure api-wise that all the necessary methods (the ones "replacing" the arguments) are used?

    Here's a very trivial example:

    Wrong way:
    PHP Code:
    public function createBox($height,$width,$color,$borderThickness,$borderColor)

    $obj->createBox('100px','100px','red','2px','blue'); 
    If I use this function and forget an argument, I will be told that an argument is missing.

    Now, please consider this:
    PHP Code:
    public function setHeight($height)
    public function 
    setWidth($width)
    public function 
    setColor($color)
    public function 
    setBorderThickness($borderThickness)
    public function 
    setBorderColor($borderColor)

    $obj->setHeight('100px')
        ->
    setWidth('100px')
        ->
    setColor('red')
        ->
    setBorderThickness('2px')
        ->
    setBorderColor('blue'
    Is there a way to make sure that all the methods in the second example are used? A sort of API-Interface?

    Regards,

    -jj.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The latter example is problematic, because it is focused on changing state of the object. You could try using a value object in this case. Eg. height and width could probably be grouped into a "dimension" object. And border-thickness and border-colour could likely be grouped as a "border" object. That puts you down to only three parameters for the main function.

    In any case, you should take a rule such as "more than 3 parameters to a function is bad" with a fair grain of salt. Some times, the alternatives are worse.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If they ALL need to be set when creating the object in the first place then they should all be there for the constructor method. Only where it makes sense to set one without another should there be separate methods to allow you to do so. Having to always call multiple methods one after the other is a good indicator that the methods should be combined into one. The is a far more important rule to follow than any arbitrary upper limit on the number of arguments a method should have (which is really only an indication that you should examine the method to see whether it really should be one method or several and not an indicator that it must be broken up).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd combine the value object kyberfabrikken mentioned and defaults. While requiring Dimension and colour to be set you could move border into a setter. To assure your box has valid border settings after construction you must set a default there.

    PHP Code:
    class Box {
        protected 
    $border// type Border
        
    protected $dim// type Dimension
        
    protected $color;

        public 
    __construct(Dimension $dim$color) {
            
    $this->dim $dim;
            
    $this->color $color;
            
    $this->border = new Border('black''1px');
        }

        public 
    setBorder(Border $border) {
            
    $this->border $border;
        }
        
    // ...

    Result: Small constructor and a clear distinction between what is necessary and what is not. If your default Border covers your average Boxes border setting you also save ::setBorder() calls.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    I don't believe this is a question of determining which methods have been called, but making sure that when such properties are necessary throwing a exception or something if they aren't all set. The other option might be requiring them when constructing the object. For example, a box can't exist without a width and height. However, it can exist without a color, border and border thickness. If that were true then upon constructing an instance of a Box the width and height might be required. However, the other properties may be optional.
    The only code I hate more than my own is everyone else's.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •