Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Feature Requests
 Refactor: create constructor for selected fields
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

RoSe
Junior Member

15 Posts

Posted - Mar 04 2008 :  03:05:23 AM  Show Profile  Reply with Quote
Sometimes I have data classes in which I define my fields and then need some constructors with different combinations of parameters that will be assigned to the fields. It would be convenient to have a refactor method on selected fields of a class like 'create constructor' that creates a constructor containing a parameter for each selected field and in the body of the constructor the parameters will be automatically assigned to the matching field.

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Mar 07 2008 :  2:48:54 PM  Show Profile  Reply with Quote
This is an interesting idea. I am in two minds about this, I can see the appeal, but I can also see some problems. My main concern is how do you select the fields you want the constructor to set?

If the class looks like:

class CTest
{
private:
    int nOne, nTwo, nThree, nFour;
};

then how do you select members one and four?

A refactoring operation to create a constructor that initialises all member variables with parameters would avoid this selection problem, but you would then need to manually delete the parameters you did not want.

zen is the art of being at one with the two'ness

Edited by - feline on Mar 07 2008 2:50:22 PM
Go to Top of Page

RoSe
Junior Member

15 Posts

Posted - Mar 10 2008 :  08:48:58 AM  Show Profile  Reply with Quote
Well, you are right. I would make the decision line by line: each line that is marked will be included with all its members. In your example that would include all four members. I think the availability of such a refactoring method will make me think of how to sort my members to ease the writing of constructors ... and that would avoid the situation you mentioned.
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Mar 10 2008 :  11:23:13 AM  Show Profile  Reply with Quote
VS2005 is not letting me select two separate lines. I can select a single line, or a block of lines, but not separate lines.

So this means that for the class:

class CTest
{
private:
    int nOne;
    int nTwo;
    int nThree;
    int nFour;
};

there is no way to select "nOne" and "nFour". You have to select just one of them, or all 4 members at once.

For a large class this would require forcing you to reorder the class members to group the members together that you need to pass to the constructor.

zen is the art of being at one with the two'ness
Go to Top of Page

accord
Whole Tomato Software

United Kingdom
3287 Posts

Posted - Mar 10 2008 :  5:50:10 PM  Show Profile  Reply with Quote
VA Outline?
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Mar 11 2008 :  09:33:56 AM  Show Profile  Reply with Quote
An interesting thought. A very interesting thought...

There is a possible conflict here. If I select three class members in VA Outline and then trigger Encapsulate Field, which is basically what we are doing here, what should happen?

Currently Encapsulate Field only operates on a single class member at a time, but there are regular requests to allow the refactoring operations to operate on several functions at once.

Is the constructor sufficiently different to normal Encapsulate Field to warrant its own special command? If you format it as:

CTest(int one, int four) : nOne(one), nFour(four)
{
}

then it probably is.

I have put in a feature request for this to see what our developers make of it. I can see this being rather useful now and then:

case=14340

zen is the art of being at one with the two'ness
Go to Top of Page

dinaiz
Junior Member

France
13 Posts

Posted - Feb 06 2012 :  9:11:00 PM  Show Profile  Reply with Quote
Hi ! I know this thread is quite old but I still think it's an important and missing feature !

IMHO, something really simple would do, like from :

class CTest
{
private:
    int nOne;
    int nTwo;
    int nThree;
    int nFour;
};


have the option to generate :

class CTest
{
private:
    int nOne;
    int nTwo;
    int nThree;
    int nFour;

public: 
Ctest(int one, int two, int three, int four) : nOne(one),nTwo(two),nTwo(three), nFour(four)
{
}

};


You can easily declare only the fileds which need initilialization, generate the constructor, then add the rest of the fields. That's what I do manually most of the time anyway :)

Or do you think there would be a way to get away with this using code snippets ?

Go to Top of Page

dinaiz
Junior Member

France
13 Posts

Posted - Feb 06 2012 :  9:25:27 PM  Show Profile  Reply with Quote
Obvisouly, something like this would be even better :
http://www.skorkin.com/2011/04/smart-constructor-generating-advanced-type-constructors-in-three-keystrokes/#.TzCLWFzj7IQ

:) :) :)
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Feb 07 2012 :  2:30:19 PM  Show Profile  Reply with Quote
An interesting link, thank you for this dinaiz. This is still a feature we are looking into adding, along with the related idea of just generating a default constructor for all members of a class.

Unfortunately I don't currently have an estimate for when this will be added to VA.

zen is the art of being at one with the two'ness
Go to Top of Page

joostn
Junior Member

16 Posts

Posted - Jan 21 2016 :  05:26:39 AM  Show Profile  Reply with Quote
I'd like to add my +1 to this request!

Just keep it simple: generate a constructor which includes all member variables in the listed order. I can remove or reorder fields afterwards.
Go to Top of Page

joostn
Junior Member

16 Posts

Posted - Jan 21 2016 :  05:34:12 AM  Show Profile  Reply with Quote
And while I'm at it, this is what I could use as well:

class T
{
private:
  int m_Member1;
  float m_Member2;
};


I often need to add a member function Tuple() as follows:

class T
{
public:
  auto Tuple() const
  {
    return std::tie(m_Member1, m_Member2);
  }
private:
  int m_Member1;
  float m_Member2;
};


This Tuple() function can be used to generate all comparison operators in one go using a mixin class. And it would probably be useful for serialization as well.

So basically I'd just need a VAX function which generates a list of all member variables separated by commas.
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Jan 21 2016 :  4:26:43 PM  Show Profile  Reply with Quote
An interesting thought, I have put in a feature request for a VA Snippet reserved string that would generate the list of class member variables, to see what our developers make of it:

case=94606

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000