Closed Thread
Results 1 to 4 of 4

Thread: Constructor vs. Setters for Instance Variables

  1. #1
    Jordan Guest

    Constructor vs. Setters for Instance Variables

    I've seen a lot of both and depending on who the developer is, either way may be the preferred method. As far as I can tell, there is no standard method. I'm talking about the way instance variable values are set when the class is created.

    Constructor Method:
    Code:
    public function __construct string $dsn,  string $usernamestring $password) {
        
    $this->__dsn         $dsn;
        
    $this->__username $username;
        
    $this->__password $password;


    Setter Method:
    Code:
    public function setDsn($dns) {
        
    $this->__dsn         $dsn;
    }

    public function 
    setUsername($username) {
        
    $this->__username $username;
    }

    public function 
    setPassword($password) {
        
    $this->__password $password;

    I prefer passing variables to the constructor. It is easier to read and less lines of code for both the object developer and the person creating the object.

    I understand there may be some instances where having it passed to the constructor and using the same setters may be beneficial, such as when you want to allow data to be changed, but for this discussion I am strictly speaking of setting the data when the class is first created.

    Which method do you prefer? Why?

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: Constructor vs. Setters for Instance Variables

    Constructors, but I think that's because they're easy to work with in C++. Especially for some types of data, there can be a serious performance hit if you use a constructor to create something large, and then redefine it with setters.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: Constructor vs. Setters for Instance Variables

    I prefer a third way, hah. In C# it is called Object Initializers. You do not have to create a constructor (default one will do) and you do not need to create methods (or property accessors). Just use a default constructor and set whatever properties you want.

    Here is some C# example.

    Code:
    class Program
    {
        class 
    PersonalInfo
        
    {
            public 
    string FullName;
            public 
    DateTime DateOfBirth;
            public 
    int Age;
        }

        static 
    void Main(string[] args)
        {
            
    PersonalInfo me = new PersonalInfo()
            {
                
    FullName "Arek Bulski",
                
    DateOfBirth = new DateTime(20090102),
                
    Age 21,
            };

            
    /// This is me. No constructor required. Neither 
            /// any method or property set accessor. :)
        
    }


  5. #4
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    89

    Re: Constructor vs. Setters for Instance Variables

    One vote for constructors..

    Posted via CodeCall Mobile

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Setters from a subclass?
    By mcallinder in forum Java Help
    Replies: 2
    Last Post: 05-17-2011, 12:41 AM
  2. Haskell even no instance for Bool
    By matio in forum General Programming
    Replies: 2
    Last Post: 04-05-2011, 11:37 AM
  3. Replies: 4
    Last Post: 03-12-2011, 09:31 PM
  4. Replies: 7
    Last Post: 11-02-2009, 03:51 PM
  5. How to get device instance id using VB6
    By LeoZao in forum Visual Basic Programming
    Replies: 1
    Last Post: 05-02-2008, 09:55 AM

Tags for this Thread

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