|T O P I C R E V I E W
||Posted - Jul 30 2013 : 08:58:39 AM
When the C++ snippet being extracted by Extract method does not reference any members, the function could be extracted as a free function. If this is not possible to detect automatically, user could be given an option to extract as a free functions. A free function would be extracted directly to the source in which extraction happens, as close above the extraction place as possible, i.e before the current function begins.
|10 L A T E S T R E P L I E S (Newest First)
||Posted - Sep 19 2014 : 12:33:16 AM
case=76113 is implemented in build 2047
||Posted - Jan 30 2014 : 9:38:50 PM
Thank you for the information and the perspective, its useful to know what people are doing. I have placed a note onto the case about this.
||Posted - Jan 28 2014 : 2:00:34 PM
I would use extract method to free function very frequently.
In my job, we prefer to keep the class definitions as small as possible. We also practice "clean code" techniques, so we end up with many many functions because functions become labels for clarifying the purpose of the code. It is preferable these functions are free functions limited in scope to the translation unit so that class definitions don't become bloated with private helper functions. We usually prefer to put them in an anonymous namespaces in the .cpp so we know they can't be extern'd from elsewhere.
My typical use case for extract method is refactoring the code to make it cleaner. Generally, 95% of the time I'm wanting to extract method to source as a free function.
Commenting because I would love to see this feature added.
||Posted - Aug 26 2013 : 8:11:40 PM
Thank you for the links. An interesting counter point to the make everything a class member.
||Posted - Aug 26 2013 : 4:40:39 PM
FWIW, there are some authors who advise embracing the muilti-paradigm (as opposed to purely OOP) nature of C++:
Scott Meyers has advocated the technique of free functions to keep class sizes down:
Also, Lakos makes some arguments for static helper functions, typically to keep dependencies and class sizes in check: http://www.amazon.com/Large-Scale-Software-Design-John-Lakos/dp/0201633620
Maybe it's worth considering separate mechanisms for "extract static function to this implementation file" versus "extract free (API) function (and declare it in the header". It makes a big difference.
||Posted - Aug 12 2013 : 8:47:29 PM
Ah, I see your problem now, thank you for the clear explanation. I have put in a feature request to allow Extract Method to create a free function. I had not thought of this problem, but its obvious when you point it out:
||Posted - Aug 12 2013 : 04:50:01 AM
I have tried it again. Most cumbersome is that when the function is declared in the header file and I extract it from it, the header file is still "changed" and all dependencies need to be rebuilt.
I cannot use "Undo" to return the header into the unchanged state, after the implementation is moved to the source file, as this undos both files simulaneously.
||Posted - Jul 30 2013 : 11:27:47 AM
Which bit is cumbersome? VA Outline is designed to help you easily move functions around in the current file, via drag and drop, or cut and paste. Alt-G will take you to the function declaration in the header file, which can then be deleted, then its just Alt-O back to the cpp file to delete the class name on the function implementation.
A few steps, but I am not sure it is worth adding this to Extract Method for occasional use.
||Posted - Jul 30 2013 : 10:22:56 AM
Not often, but when I do, doing it with current functionality is a bit cumbersome.
A most common example is a small computation function, like in your example of AreaOfCircle in http://www.wholetomato.com/products/features/extractmethod.asp
||Posted - Jul 30 2013 : 09:40:56 AM
Is this something you often want to do? Most people seem to want to put as much code as possible into functions, minimising the number of free functions.