Author |
Topic |
|
andre.horst
Tomato Guru
Germany
150 Posts |
Posted - Oct 09 2008 : 10:54:38 AM
|
I'd like to have a feature to move a implementation back to the header where the method is already declared. I require this feature after optimizing code and than if the method consist of only a few lines i want to have it "inlined" in the header.
The "Extract Method" moves the selected code to the header and creates a new method declaration. Therfore it is not that usefull.
f.e.: In the header: void foo() ;
In the cpp: void foo() { // Some code }
Now select the method "foo" and move it back to header via "Move implementation to header", that results in the header as followed:
inline void foo() { // Some code }
|
|
feline
Whole Tomato Software
United Kingdom
19020 Posts |
Posted - Oct 10 2008 : 7:33:10 PM
|
We are considering adding this command / feature:
case=10239 |
zen is the art of being at one with the two'ness |
|
|
sdfox
New Member
USA
4 Posts |
Posted - Jan 07 2014 : 3:00:19 PM
|
I would really like to see this feature implemented. Sometimes engineers are working on projects with no modeling tool support, and it is really helpful if we have a feature to create inline functions from attributes. What I am asking for might be slightly different but it is related to inline function creation. |
|
|
feline
Whole Tomato Software
United Kingdom
19020 Posts |
Posted - Jan 09 2014 : 1:37:27 PM
|
We are still looking to add this refactoring command, and are hoping to get to it relatively soon.
I am not sure what you mean about inline functions from attributes, are you talking about Encapsulate Field? This generates a pair of functions, to set and get, from a member variable in a class:
http://docs.wholetomato.com/default.asp?W156 |
zen is the art of being at one with the two'ness |
|
|
jay.carlton
Ketchup Master
USA
65 Posts |
Posted - Jan 31 2014 : 3:51:37 PM
|
I think there are several potentially related feature requests that I've either read about or just thought of involving the destination of Extract Method. My list of potential destinations is - class method (private or public)
- free function local to this cpp file (static or unnamed namespace
- free function in another header for export
- inline in this class's header
- lambda
Just my two cents.
-Jay |
|
|
feline
Whole Tomato Software
United Kingdom
19020 Posts |
Posted - Feb 04 2014 : 1:35:34 PM
|
We are considering adding an option to tell Extract Method to make a free function, instead of a class member function:
case=76113
Currently, to control if the new method ends up in the cpp file or stays in the header file, just set the "Extract to Source" check box to on or off, depending on what you are after.
I don't really know anything about lambda yet, but I thought this was something you did within a function, not a new type of function, so can you explain what you are talking about here please? |
zen is the art of being at one with the two'ness |
|
|
jay.carlton
Ketchup Master
USA
65 Posts |
Posted - Feb 04 2014 : 2:15:51 PM
|
The point of a lambda for me is to remove the distinction between functions and objects, and to separate within an algorithm the concepts of what to do and when to do it.
I kinda included that one to stimulate conversation, although now that I think about it, I'm surprised Extract Method doesn't work this way internally already. In other words, creating a lambda could be the first step in extracting a new function, and this would help solve the ambiguity of missing variable declarations in the highlighted code (i.e., either treat them as captures by value or reference, or make them parameters).
I suppose such a refactoring would extract a chunk of code, including captures and argument list, into an anonymous function. So it's like making a new function but stuffing it into a lambda object instead of a separate method. You'd probably need a way to say which variables appearing in the chunk were captures and which were parameters.
This kind of refactoring would be helpful in transforming hand-coded algorithms and loops to use std algorithms.
Looks like a pretty good intro for lambdas in C++11 is at http://www.cprogramming.com/c++11/c++11-lambda-closures.html
For a much better discussion of the theory than I can provide, see http://mitpress.mit.edu/sicp/full-text/book/book.html
There's an MSDN article at http://msdn.microsoft.com/en-us/library/dd293608.aspx with some examples of the different styles.
There are two or three different ways to declare these function objects, either as std::function, lambda, or with auto. You're probably right in that the target is more often within a function, but the essence of the transformation is the same.
I'll try to think of situations that would really benefit practically from this. The most common place I used them was std::for_each<>, but now that VS 2012 supports range based for loops, I have to confess I don't use them as often.
Edit: almost forgot about C#, although I've yet to try this there. http://msdn.microsoft.com/en-us/library/bb397687.aspx
|
Edited by - jay.carlton on Feb 04 2014 2:44:02 PM |
|
|
feline
Whole Tomato Software
United Kingdom
19020 Posts |
Posted - Feb 27 2014 : 2:45:08 PM
|
The first thought about Extract Method and Lambda is that Extract Method works in VC6, so it "grew up" in a Lambda free world I don't know the internal details of how Extract Method generates the code, but I do know you run into all sorts of fun edge cases regardless of the approach you take. Its amazing the "fun" things you can do in C++, especially with macros
What problem are you trying to solve with not selecting the variable declaration? If the variable declaration is not selected, then either we need to be quite clever, or we need to to pass it as a parameter. The main complaint I see is that people did not select the variable declaration, but did not want the variable passed as a parameter, since all uses are inside the selected code. This is a case we are looking to handle specifically:
case=63055
I have only scanned over the links you provided so far, thank you for these, but its clear that Lambda functions take parameters, so making the extracted code a Lambda doesn't seem to change the need to figure out your parameters at all, unless I am missing something fundamental here. That's quite possible, especially since this is a new area for me
I have put in a feature request for Extract Method to support Lambda. This may or may not be easy, we will know more when our developers have had a good look at this:
case=80567 |
zen is the art of being at one with the two'ness |
|
|
jay.carlton
Ketchup Master
USA
65 Posts |
Posted - Feb 27 2014 : 3:51:31 PM
|
I claim that you're you're already doing Extract to Lambda, but that you're going further and not stopping when you have a lambda but instead adding a name to make it a function. From the CS standpoint, all functions (and arguably objects too) are lambdas, right? (Granted, in C++ the way scoping works isn't the same as in Lisp. But you're doing the heavy lifting already, the rest is just destination syntax and scope. And you have expertise in determining scope.)
The trick with the parameters is that our standard error handling scheme has evolved such that pretty much all of a function's local variables wind up at the beginning of the function, even for very long functions. So it's commonly the case that the region I want to extract has locals in it that will be mis-identified as parameters that need to come in. So maybe if I had a Filter or Advanced step in Extract Method to select which variables are locals (and to retrieve their earlier declaration somehow), which are parameters, what order I want the parameters in, which are return values (as opposed to output or inout parameters), etc. I don't expect VA to guess all these things, especially since I might not have my mind made up by the time I run the tool.
-Jay |
|
|
feline
Whole Tomato Software
United Kingdom
19020 Posts |
Posted - Feb 27 2014 : 4:19:28 PM
|
I don't feel I know enough about Lambda's to comment here. What little I understand so far makes them look like "normal" functions, the difference is in how they are called. They still look like valid self contained blocks of code, which is what we are interested in producing with Extract Method
For the parameters, would having VA detect that all references to the local variable have been selected help? This is what case=63055 is about. If you have selected all references except for the declaration, then its safe to grab and move the declaration as well, and this variable becomes a local variable in the new function.
As you say, variables that are used outside of the selected code need to be considered on a case by case basis. |
zen is the art of being at one with the two'ness |
|
|
jay.carlton
Ketchup Master
USA
65 Posts |
Posted - Feb 27 2014 : 10:17:45 PM
|
I believe you can name them and call them like functions as well, for what that's worth.
I'm still learning too, so I'll explain it to myself here and see if any CS professors want to chime in and correct my ramblings. I found a good talk by Herb Sutter at https://www.youtube.com/watch?v=rcgRY7sOA58 but I still can't find the diagram I'm looking for that shows a functor object representing a lambda. I think it looked like
struct LambdaFunctor
{
public:
LambdaFunctor(int &capture1, int &capture2) : m_capture1(capture1), m_capture2(capture2) {}
operator() (int x) { m_capture1 * x + m_capture2;}
private:
int m_capture1;
int m_capture2;
};
// equivalent lambda
int capture1 = 2;
int capture2 = 3; // or whatever, the point is it's in the surrounding scope
auto theRealLambda = [&] (x) { capture1 * x + capture2; }
LambdaFunctor fnObj(2,3);
fnObj(5); // 13, if I'm doing the math right
theRealLambda(5); // 13
So all it is really is shorthand for declaring one of those: an object that acts like a function that has instance data captured from its surrounding scope.
The point to all this isn't just another way to use Extract Method, but that it's another step in the transition from a text editor operating on source code to a software editor operating on the higher level but somehow even more fundamental concepts. The reasons I'd like to see a tool treat functions and lambdas as interchangable flavors of the same thing have more to do with high-level pattern recognition and extraction. Not to mention that getting that right might also make all the other method extraction options I listed simpler to do. Once you've got that, then it becomes possible to talk about finding functions that are better expressed as compositions of smaller functions, and suggesting refactorings proactively. More ways to focus on programming without thinking about code.
W/r/t parameters, yes, if it could detect whether parameters were either (1) completely internal to the selection, (2) declared above but not used otherwise or (3) set but not read after that in the selection, than that could be a hint to picking out local variables (1-2) and return values. Sounds like you're already doing some of that though. Maybe Extract Method could support a non-contiguous-highlight invocation (i.e. action-object instead of object-action) and when invoked, let me pick which bits are parameters, which are locals, and what constitutes the body.
-Jay |
|
|
sean
Whole Tomato Software
USA
2817 Posts |
Posted - Sep 19 2014 : 12:33:28 AM
|
case=76113 is implemented in build 2047 |
|
|
sean
Whole Tomato Software
USA
2817 Posts |
Posted - Jul 18 2016 : 10:50:59 PM
|
case=10239 is implemented in build 2107. |
Edited by - sean on Jul 18 2016 10:52:27 PM |
|
|
|
Topic |
|