Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm probably butchering the terms, if someone knows the appropriate terms - that'd be great.

But my question is: should I design my classes so that their methods' parameters be accessible from the outside, or should they be all self contained?

For example, I want to set a domain name for a class, which will served as the 'active' domain:

function setDomain($url){
        if ($this -> domainUrl = parse_url($url, PHP_URL_HOST)){
            $this-> domain = $this->db->get_where('domains', array('host' => $this->domainUrl));

            return $this;
        }

        return false;
    }

and then go the route of:

function addDomain(){
        $query = "INSERT INTO domains (host) VALUES ('$this->domainUrl')";
        $this->db->query($query);
        return $this
    }

or I can abandon the entire route of setDomain(), and than just call the url explicitly, such as:

function addDomain($url){
        $query = "INSERT INTO domains (host) VALUES ('$url')";
        $this->db->query($query);
    }

Which would you say is preferable and I guess - what is that dependent upon, because there might be cases where I'll need to call it explicitly.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Generally only set attributes on the instance if:

  • you will be making many calls with those exact arguments,
  • the parameters need to be collected from various places for final processing, or
  • there is so many arguments that you need to name them.

Otherwise passing an argument is less code and less code is more readable. Also the compiler/interpreter will tell you when you missed argument, while you have to check yourself when you forgot to call a setter.

share|improve this answer
add comment

As with any general design question, the answer to this question is also it depends. It all depends on your abstraction for the class.

Let me give two examples that illustrate two different abstractions:

  • class DomainOperator: This highly fictitious class takes care of performing various operations on a domain. Given a domain, it can store it, make some String-relevant operations on it, etc... For this class, I would go declaring the domainURL as an instance variable. Multiple methods should be making use of it; it acts as a global within a class.

  • class WebSite: Yet another made-up class that is responsible for storing the properties of a web-site; domain is one of these properties. It has a method called addDomain(url) and in this method, it stores the domain.

    I am not too happy with these examples, but the point is that it depends on what you want to do with your class. If your class logic is based on a domain, then I would make it an instance variable. If not, a parameter would be fine.

    Side note: These small decisions become easier with refactoring. If you look at a class and you see 4 methods (out of 7) using a variable, then it might be time to extract these 4 methods out into a new class; making the shared variable an instance variable of the new class.

  • share|improve this answer
    add comment

    Your Answer

     
    discard

    By posting your answer, you agree to the privacy policy and terms of service.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.