SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Many functions or many options? How to pass options/parameters

    I am going to use this as an example. Lets say I have a method/function that I want to a mask a number, with the possiblity of the following options:

    1. Hide x digits
    2. Show x digits
    3. Hide digits starting from the right or left
    4. Show digits from the left or the right

    Should I write one method/function to do all this, or write several methods/functions that specialize in one thing. Also, how should I pass the options to the method/function. ie, function(1) with 1 = show, 2 = hide
    or function('hide') or function('show'), or does someone have a better suggestion?

  2. #2
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To keep things simple, write methods/function that do one thing, then write a function which then use these other functions to do a collection of things.

    As for the parameters, you could get away with two boolean parameters - one for either showing or hiding x digits and the other for either showing or hiding the digits from right or left.

    In short, rather than using 1 and 2 for options, use 0 and 1 (boolean), since you don't waste memory that way.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Or use constants instead of cryptic 0 or 1. This makes it easy for anyone or yourself remember what the second param does.

    PHP Code:
    define('DIRECTION_LEFT',  0);
    define('DIRECTION_MID',   1);
    define('DIRECTION_RIGHT'2);

    function 
    something ($var$dir DIRECTION_RIGHT) {
        switch (
    $dir) {
            case 
    DIRECTION_LEFT:break;
            case 
    DIRECTION_MID:break;
            case 
    DIRECTION_RIGHT:break;
        }

    Also using 0 or 1 over 1 or 2 does not save any more memory. PHP receives both as integers.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, use false and true then (or are they translated into integers too?).

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    This may or may not be working hard to say. But I'm guessing memory_get_usage doesn't work on Windows because this small test gives the same amount for all except $a = 1

    PHP Code:
    <?php

    header
    ('content-type: text/plain');

    $a 1;

    echo 
    memory_get_usage() . "\n";

    $a 2;

    echo 
    memory_get_usage() . "\n";

    $a true;

    echo 
    memory_get_usage() . "\n";

    $a 1;

    echo 
    memory_get_usage() . "\n";

    $a 'string';

    echo 
    memory_get_usage() . "\n";
    But it really is not a concern the use of constants for more complex things like this function would benefit using that design.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •