+1

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: vrijdag 8 februari 2002 12:28
To: [EMAIL PROTECTED]
Subject: [Eap-features] Move local declaration out of block


If I have

      Session sess = getSession();
      sess.process(data);
      sess.close();

Now I'm worried that 'process()' might fail, so I surround the lot with a
try..catch, and add some code in the catch

      try {
            Session sess = getSession();
            sess.process(data);
            sess.close();
      }
      catch (SessionException e) {
            //  I type this...
            sess = getDefaultSession();
      }

I'd like IDEA to automatically spot that I've used 'sess' (declared inside
the try block) and show a light bulb to ask me if I'd like to move the
declaration of sess outside the block, and ask me for a default value
(which could either be null or the expression used to initialise "sess").

The same thing would happen if I did this in a finally block; imagine I
copied the 'sess.close()' part into the finally block -- sess is used but
not defined, so IDEA prompts to move the declaration out.

In fact, this might work in general situations.  If a local variable is
declared inside a block, but used outside it, IDEA could prompt to move the
declaration outside as many blocks as is necesary to bring it into scope.
So in the general case:

      {
            if (condition) {
                  String my_string = getValue();
            }
      }
      // Syntax error; my_string is not defined.
      return my_string;

IDEA prompts to 'Move my_string into scope?' when I type the return
statement.  If I say OK, the code becomes

      String my_string = null;
      {
            if (condition) {
                  my_string = getValue();
            }
      }
      // No syntax errors now.
      return my_string;

I the other option on the light bulb menu is to add another declaration of
String my_string.   In this case, the code becomes

      {
            if (condition) {
                  String my_string = getValue();
            }
      }
      // No syntax errors now, but probably not what I meant
      String my_string = null;
      return my_string;


What do you think?  I've had to do this about 20 times manually today, It
would be a real time-saver if IDEA did it automatically.

Cheers,
Ben.




_______________________________________________
Eap-features mailing list
[EMAIL PROTECTED]
http://www.intellij.com/mailman/listinfo/eap-features

_______________________________________________
Eap-features mailing list
[EMAIL PROTECTED]
http://www.intellij.com/mailman/listinfo/eap-features

Reply via email to