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
 Split Declaration refactoring
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

legalize
Tomato Guru

USA
119 Posts

Posted - Mar 27 2013 :  2:06:30 PM  Show Profile  Reply with Quote
Summary: When multiple identifiers are declared in a single statement, split the identifier containing the cursor into a separate declaration

Example:

Source code reads:
int i, j;
and the cursor is anywhere between the comma and the semi-colon and I select "Split Declaration", the code becomes
int i;
int j;

Rationale:

A common refactoring activity is to perform "Compose Method"[1] on long methods and functions.

Long methods often have lots of local state that is stored in variables defined in the local scope. Often times, the declarations are grouped together because the programmer already recognized that this method was really long and they attemped to conserve space by declaring multiple variables on the same line. However, one variable is used in one area of the method and another variable is used in a second area of the method.

When performing Compose Method, these variables are going to end up as local variables in the extracted submethods, but first we need to localize their use or Extract Method will consider them modifiable parameters of the original scope that need to be passed in and out of the extracted methods.

So, before we can get VAX's Extract Method to do something useful for us (remember, we want to use automated tools to change the code in order to avoid the mistakes that come from making manual changes to code), we need to localize the variables used in each block we intend to extract. That means splitting multiple declarations into individual declarations and then moving those declarations to immediately before their first use.

[1] "Compose Method" refactoring:
http://www.industriallogic.com/xp/refactoring/composeMethod.html

See "Refactoring to Patterns", by Joshua Kerievsky; if you haven't read this book yet and you're implementing a refactoring tool, you should stop reading this forum thread and go order the book.

http://legalizeadulthood.wordpress.com

feline
Whole Tomato Software

United Kingdom
18952 Posts

Posted - Mar 27 2013 :  2:59:07 PM  Show Profile  Reply with Quote
Are you likely to want or need to split the declarations when you are not moving them around? If you have 6 or 8 variables declared on the same line, I assume you are looking for this to become 6 or 8 separate lines, one variable per line.

This makes sense, I am just wondering how useful it is on its own, or if it should just be rolled into part of your Localize Variable refactoring suggestion:

http://forums.wholetomato.com/forum/topic.asp?TOPIC_ID=11305

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

legalize
Tomato Guru

USA
119 Posts

Posted - Mar 28 2013 :  11:23:38 AM  Show Profile  Reply with Quote
Yes, it's useful on it's own. I've done this hundreds of times when migrating legacy C code with long functions to C++, because C forces you to declare a variable at the most recently created {} block and lots of people didn't realize they could introduce a new scope block anytime they wanted in C, so for long functions there tends to be a sh'load of variables declared at the top of the function, even then they are only used locally much farther down.

Given the difficulty of the flow analysis and parsing, I'm trying to make suggestions for new refactorings that are simple, small steps. While it's true that Split Declaration and Localize Variable are often used together, it's easier on the tool developer if I apply those two separately as a user rather than trying to combine them into a larger refactoring that's harder to implement correctly.

It's also the case that I often want to apply these refactorings individually. Sometimes I have code that suffers from the bulk declarations and other times I have code that doesn't suffer from bulk declarations but the declarations aren't localized to the first use.

http://legalizeadulthood.wordpress.com
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18952 Posts

Posted - Mar 29 2013 :  3:47:56 PM  Show Profile  Reply with Quote
This all makes sense. It turns out this idea has come up before, but only the once by the looks of it. I have added your comments to this case, since you made made good points here:

case=30486

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