Split into declaration and assignment - why??

There is a refactoring suggestion that IntelliJ proposes that I dont understand the benefit of (unusual, considering most intellij features are very useful).

For the following code:


IntelliJ suggests (via the lightbulb)

"Split into declaration and assignment"

Why would I want to bother doing this? What is the rationale behind this sort of refactoring?
I must be missing something...

-Nick

10 comments
Comment actions Permalink

Imagine this scenario:

int i = Integer.parseInt(str);

but upon further thought you realize that this can throw a
NumberFormatException, and when this happens you want to print a warning
and use a default value.
try {
int i = Integer.parseInt(str);
}
catch (NumberFormatException e) {
i = DEFAULT_VALUE;
System.out.println("Invalid value [" + str + "], Using default");
}
// Code that uses i goes here

But the problem is that i is only valid within the block in which it is
declared. Therefore, what you want is this:

int i;
try {
i = Integer.parseInt(str);
}
catch (NumberFormatException e) {
System.out.println("Invalid value [" + str + "]; Using default");
i = DEFAULT_VALUE;
}

This "intention" (refactoring suggestion) helps in these situations.

Nick Minutello wrote:

There is a refactoring suggestion that IntelliJ proposes that I dont understand the benefit of (unusual, considering most intellij features are very useful).

For the following code:



IntelliJ suggests (via the lightbulb)

"Split into declaration and assignment"

Why would I want to bother doing this? What is the rationale behind this sort of refactoring?
I must be missing something...

-Nick


0
Comment actions Permalink

Sorry to contradict you Matthew, but you don't need it there :
"surround with" + "try-catch"
will do the job for you in one simple step.


Before :
-


|int i = Integer.parseInt("11");
System.out.println( "i = " + i );

Action :
-


1? : "surround with" (Alt-Ctrl-T)
2* : select try-catch

After :
-


int i = 0;
try {
i = Integer.parseInt("11");
}
catch ( NumberFormatException e ) {
e.printStackTrace();
}
System.out.println( "i = " + i );

0
Comment actions Permalink

LOL. You are right. What an amazing program!

Alain Ravet wrote:

Sorry to contradict you Matthew, but you don't need it there :
"surround with" + "try-catch"
will do the job for you in one simple step.


Before :
--------
|int i = Integer.parseInt("11");
System.out.println( "i = " + i );

Action :
--------
1? : "surround with" (Alt-Ctrl-T)
2* : select try-catch

After :
--------
int i = 0;
try {
i = Integer.parseInt("11");
}
catch ( NumberFormatException e ) {
e.printStackTrace();
}
System.out.println( "i = " + i );


0
Comment actions Permalink


>> LOL. You are right. What an amazing program

I agree ;)

Its because of that very feature that I dont understand the need for "Split into declaration and assignment"...

-Nick

0
Comment actions Permalink

Alain Ravet wrote:

Sorry to contradict you Matthew, but you don't need it there :
"surround with" + "try-catch"
will do the job for you in one simple step.


I guess in theory you could first have declared the variable inside the
try-catch and then you realize that you need its value after the
try-catch too, or in the catch statement. Then "surround with" doesn't
help you, but you could use the split intention as a first step and then
move the new declaration line to the outer scope.

But I don't think this has ever happened to me.

0
Comment actions Permalink

"Nick Minutello" <jiveadmin@jetbrains.com> wrote in message
news:6686209.1037563103675.JavaMail.jrun@is.intellij.net...

>> LOL. You are right. What an amazing program

>

I agree ;)

>

Its because of that very feature that I dont understand the
need for "Split into declaration and assignment"...


IDEA also has the reverse -- sort of:

String ice|;
ice = "cream";

Pressing ctrl-J ("join lines") will result in:

String ice = "cream";

instead of the less useful:

String ice; ice = "cream";

that you'd expect. Pretty cool. It would be nice if it could handle this,
though:

String ice = null;
doFoo();
doBar();
ice = "cream";

==>

doFoo();
doBar();
String ice = "cream";

but as far as I know, it can't.




0
Comment actions Permalink

But I don't think this has ever happened to me.


I've used it a few times in this senario & have found this refactoring useful!

0
Comment actions Permalink

Sorry to contradict you Matthew, but you don't need it there :
"surround with" + "try-catch"
will do the job for you in one simple step.

Before :
--------
|int i = Integer.parseInt("11");
System.out.println( "i = " + i );


To obtain the behavior described, it is necessary, that the variable is
used thereafter, that is, with the second line removed, Idea just comes
up with

try {
int i = ...
} catch ...

Sometimes I would like this "cleverness" turned of and have the split
done before in any case, as I use the functionality most often when just
developing a methode for the first time.

Karsten
--
Karsten Tinnefeld tinnefeld@adesso.de
adesso AG
Stockholmer Allee 24 T +49 231 9309211
44269 Dortmund, Deutschland M +49 172 4542646 F +49 231 9309331

0
Comment actions Permalink

I have used it in this scanario if the try catch allready exist and I just want to move a new item in to the block.
There may be a fancier way to do it but split + drag and drop work very well :)

0
Comment actions Permalink

Joey Edelstein wrote:
> There may be a fancier way to do it but split + drag and drop work
very well :)

Yes, there is :
split + move the line up with
with the LineUpAndDownMoverPlugin :
http://www.intellij.org/twiki/bin/view/Main/LineUpAndDownMoverPlugin

Alain Ravet

0

Please sign in to leave a comment.